gecko-dev/image/ImageUtils.h
Andrew Osmond 874ce61a83 Bug 1901078 - Implement promise based anonymous image decoder. r=tnikkel
This bypasses any caching used on the display pipeline and is
intended to be used by layers that want asynchronous decoding
that fits well into the MozPromise style.

This also fixes an issue with the frame count metadata decoder in the
GIF decoder. The code only began being used with this patch, and the WPT
exposed an overflow bug caused by not clearing nsGIFDecoder2::mColormap
and nsGIFDecoder2::mColormapSize.

Differential Revision: https://phabricator.services.mozilla.com/D212833
2024-07-24 03:16:16 +00:00

142 lines
3.4 KiB
C++

/* -*- 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 "mozilla/image/SurfaceFlags.h"
#include "mozilla/Assertions.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,
BMP,
BMP_CLIPBOARD,
ICO,
ICON,
WEBP,
AVIF,
JXL,
UNKNOWN
};
struct DecodeMetadataResult {
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,
SurfaceFlags aSurfaceFlags);
static DecoderType GetDecoderType(const nsACString& aMimeType);
private:
ImageUtils() = delete;
~ImageUtils() = delete;
};
} // namespace image
} // namespace mozilla
#endif // mozilla_image_ImageUtils_h