1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_image_ImageUtils_h
#define mozilla_image_ImageUtils_h
#include "FrameTimeout.h"
#include "Orientation.h"
#include "mozilla/image/SurfaceFlags.h"
#include "mozilla/Maybe.h"
#include "mozilla/MozPromise.h"
#include "mozilla/RefPtr.h"
#include "mozilla/ThreadSafeWeakPtr.h"
#include "nsString.h"
#include "nsTArray.h"
namespace mozilla {
class ErrorResult;
namespace gfx {
class SourceSurface;
}
namespace image {
class Decoder;
class imgFrame;
class ImageMetadata;
class SourceBuffer;
/**
* The type of decoder; this is usually determined from a MIME type using
* DecoderFactory::GetDecoderType() or ImageUtils::GetDecoderType().
*/
enum class DecoderType {
PNG,
GIF,
JPEG,
JPEG_PDF,
BMP,
BMP_CLIPBOARD,
ICO,
ICON,
WEBP,
AVIF,
JXL,
UNKNOWN
};
struct DecodeMetadataResult {
CopyableTArray<OrientedIntSize> mNativeSizes;
int32_t mWidth = 0;
int32_t mHeight = 0;
int32_t mRepetitions = -1;
uint32_t mFrameCount = 0;
bool mAnimated = false;
bool mFrameCountComplete = true;
};
struct DecodeFrameCountResult {
uint32_t mFrameCount = 0;
bool mFinished = false;
};
struct DecodedFrame {
RefPtr<gfx::SourceSurface> mSurface;
FrameTimeout mTimeout;
};
struct DecodeFramesResult {
nsTArray<DecodedFrame> mFrames;
bool mFinished = false;
};
using DecodeMetadataPromise = MozPromise<DecodeMetadataResult, nsresult, true>;
using DecodeFrameCountPromise =
MozPromise<DecodeFrameCountResult, nsresult, true>;
using DecodeFramesPromise = MozPromise<DecodeFramesResult, nsresult, true>;
class AnonymousMetadataDecoderTask;
class AnonymousFrameCountDecoderTask;
class AnonymousFramesDecoderTask;
class AnonymousDecoder : public SupportsThreadSafeWeakPtr<AnonymousDecoder> {
public:
virtual RefPtr<DecodeMetadataPromise> DecodeMetadata() = 0;
virtual void Destroy() = 0;
virtual RefPtr<DecodeFrameCountPromise> DecodeFrameCount(
uint32_t aKnownFrameCount) = 0;
virtual RefPtr<DecodeFramesPromise> DecodeFrames(size_t aCount) = 0;
virtual void CancelDecodeFrames() = 0;
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
virtual const char* typeName() const = 0;
virtual size_t typeSize() const = 0;
#endif
virtual ~AnonymousDecoder();
protected:
AnonymousDecoder();
// Returns true if successfully initialized else false.
virtual bool Initialize(RefPtr<Decoder>&& aDecoder) = 0;
virtual void OnMetadata(const ImageMetadata* aMetadata) = 0;
virtual void OnFrameCount(uint32_t aFrameCount, bool aComplete) = 0;
// Returns true if the caller should continue decoding more frames if
// possible.
virtual bool OnFrameAvailable(RefPtr<imgFrame>&& aFrame,
RefPtr<gfx::SourceSurface>&& aSurface) = 0;
virtual void OnFramesComplete() = 0;
friend class AnonymousMetadataDecoderTask;
friend class AnonymousFrameCountDecoderTask;
friend class AnonymousFramesDecoderTask;
};
class ImageUtils {
public:
static already_AddRefed<AnonymousDecoder> CreateDecoder(
SourceBuffer* aSourceBuffer, DecoderType aType,
const Maybe<gfx::IntSize>& aOutputSize, SurfaceFlags aSurfaceFlags);
static DecoderType GetDecoderType(const nsACString& aMimeType);
private:
ImageUtils() = delete;
~ImageUtils() = delete;
};
} // namespace image
} // namespace mozilla
#endif // mozilla_image_ImageUtils_h
|