michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_IMAGELIB_IMAGE_H_ michael@0: #define MOZILLA_IMAGELIB_IMAGE_H_ michael@0: michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include "imgIContainer.h" michael@0: #include "imgStatusTracker.h" michael@0: #include "ImageURL.h" michael@0: michael@0: class nsIRequest; michael@0: class nsIInputStream; michael@0: michael@0: namespace mozilla { michael@0: namespace image { michael@0: michael@0: class Image : public imgIContainer michael@0: { michael@0: public: michael@0: // Mimetype translation michael@0: enum eDecoderType { michael@0: eDecoderType_png = 0, michael@0: eDecoderType_gif = 1, michael@0: eDecoderType_jpeg = 2, michael@0: eDecoderType_bmp = 3, michael@0: eDecoderType_ico = 4, michael@0: eDecoderType_icon = 5, michael@0: eDecoderType_unknown = 6 michael@0: }; michael@0: static eDecoderType GetDecoderType(const char *aMimeType); michael@0: michael@0: /** michael@0: * Flags for Image initialization. michael@0: * michael@0: * Meanings: michael@0: * michael@0: * INIT_FLAG_NONE: Lack of flags michael@0: * michael@0: * INIT_FLAG_DISCARDABLE: The container should be discardable michael@0: * michael@0: * INIT_FLAG_DECODE_ON_DRAW: The container should decode on draw rather than michael@0: * decoding on load. michael@0: * michael@0: * INIT_FLAG_MULTIPART: The container will be used to display a stream of michael@0: * images in a multipart channel. If this flag is set, INIT_FLAG_DISCARDABLE michael@0: * and INIT_FLAG_DECODE_ON_DRAW must not be set. michael@0: */ michael@0: static const uint32_t INIT_FLAG_NONE = 0x0; michael@0: static const uint32_t INIT_FLAG_DISCARDABLE = 0x1; michael@0: static const uint32_t INIT_FLAG_DECODE_ON_DRAW = 0x2; michael@0: static const uint32_t INIT_FLAG_MULTIPART = 0x4; michael@0: michael@0: /** michael@0: * Creates a new image container. michael@0: * michael@0: * @param aMimeType The mimetype of the image. michael@0: * @param aFlags Initialization flags of the INIT_FLAG_* variety. michael@0: */ michael@0: virtual nsresult Init(const char* aMimeType, michael@0: uint32_t aFlags) = 0; michael@0: michael@0: virtual already_AddRefed GetStatusTracker() = 0; michael@0: virtual void SetStatusTracker(imgStatusTracker* aStatusTracker) {} michael@0: michael@0: /** michael@0: * The rectangle defining the location and size of the given frame. michael@0: */ michael@0: virtual nsIntRect FrameRect(uint32_t aWhichFrame) = 0; michael@0: michael@0: /** michael@0: * The size, in bytes, occupied by the significant data portions of the image. michael@0: * This includes both compressed source data and decoded frames. michael@0: */ michael@0: virtual uint32_t SizeOfData() = 0; michael@0: michael@0: /** michael@0: * The components that make up SizeOfData(). michael@0: */ michael@0: virtual size_t HeapSizeOfSourceWithComputedFallback(mozilla::MallocSizeOf aMallocSizeOf) const = 0; michael@0: virtual size_t HeapSizeOfDecodedWithComputedFallback(mozilla::MallocSizeOf aMallocSizeOf) const = 0; michael@0: virtual size_t NonHeapSizeOfDecoded() const = 0; michael@0: virtual size_t OutOfProcessSizeOfDecoded() const = 0; michael@0: michael@0: virtual void IncrementAnimationConsumers() = 0; michael@0: virtual void DecrementAnimationConsumers() = 0; michael@0: #ifdef DEBUG michael@0: virtual uint32_t GetAnimationConsumers() = 0; michael@0: #endif michael@0: michael@0: /** michael@0: * Called from OnDataAvailable when the stream associated with the image has michael@0: * received new image data. The arguments are the same as OnDataAvailable's, michael@0: * but by separating this functionality into a different method we don't michael@0: * interfere with subclasses which wish to implement nsIStreamListener. michael@0: * michael@0: * Images should not do anything that could send out notifications until they michael@0: * have received their first OnImageDataAvailable notification; in michael@0: * particular, this means that instantiating decoders should be deferred michael@0: * until OnImageDataAvailable is called. michael@0: */ michael@0: virtual nsresult OnImageDataAvailable(nsIRequest* aRequest, michael@0: nsISupports* aContext, michael@0: nsIInputStream* aInStr, michael@0: uint64_t aSourceOffset, michael@0: uint32_t aCount) = 0; michael@0: michael@0: /** michael@0: * Called from OnStopRequest when the image's underlying request completes. michael@0: * michael@0: * @param aRequest The completed request. michael@0: * @param aContext Context from Necko's OnStopRequest. michael@0: * @param aStatus A success or failure code. michael@0: * @param aLastPart Whether this is the final part of the underlying request. michael@0: */ michael@0: virtual nsresult OnImageDataComplete(nsIRequest* aRequest, michael@0: nsISupports* aContext, michael@0: nsresult aStatus, michael@0: bool aLastPart) = 0; michael@0: michael@0: /** michael@0: * Called for multipart images to allow for any necessary reinitialization michael@0: * when there's a new part to add. michael@0: */ michael@0: virtual nsresult OnNewSourceData() = 0; michael@0: michael@0: virtual void SetInnerWindowID(uint64_t aInnerWindowId) = 0; michael@0: virtual uint64_t InnerWindowID() const = 0; michael@0: michael@0: virtual bool HasError() = 0; michael@0: virtual void SetHasError() = 0; michael@0: michael@0: virtual ImageURL* GetURI() = 0; michael@0: }; michael@0: michael@0: class ImageResource : public Image michael@0: { michael@0: public: michael@0: already_AddRefed GetStatusTracker() MOZ_OVERRIDE { michael@0: nsRefPtr statusTracker = mStatusTracker; michael@0: MOZ_ASSERT(statusTracker); michael@0: return statusTracker.forget(); michael@0: } michael@0: void SetStatusTracker(imgStatusTracker* aStatusTracker) MOZ_OVERRIDE MOZ_FINAL { michael@0: MOZ_ASSERT(aStatusTracker); michael@0: MOZ_ASSERT(!mStatusTracker); michael@0: mStatusTracker = aStatusTracker; michael@0: } michael@0: virtual uint32_t SizeOfData() MOZ_OVERRIDE; michael@0: michael@0: virtual void IncrementAnimationConsumers() MOZ_OVERRIDE; michael@0: virtual void DecrementAnimationConsumers() MOZ_OVERRIDE; michael@0: #ifdef DEBUG michael@0: virtual uint32_t GetAnimationConsumers() MOZ_OVERRIDE { return mAnimationConsumers; } michael@0: #endif michael@0: michael@0: virtual void SetInnerWindowID(uint64_t aInnerWindowId) MOZ_OVERRIDE { michael@0: mInnerWindowId = aInnerWindowId; michael@0: } michael@0: virtual uint64_t InnerWindowID() const MOZ_OVERRIDE { return mInnerWindowId; } michael@0: michael@0: virtual bool HasError() MOZ_OVERRIDE { return mError; } michael@0: virtual void SetHasError() MOZ_OVERRIDE { mError = true; } michael@0: michael@0: /* michael@0: * Returns a non-AddRefed pointer to the URI associated with this image. michael@0: * Illegal to use off-main-thread. michael@0: */ michael@0: virtual ImageURL* GetURI() MOZ_OVERRIDE { return mURI.get(); } michael@0: michael@0: protected: michael@0: ImageResource(ImageURL* aURI); michael@0: michael@0: // Shared functionality for implementors of imgIContainer. Every michael@0: // implementation of attribute animationMode should forward here. michael@0: nsresult GetAnimationModeInternal(uint16_t *aAnimationMode); michael@0: nsresult SetAnimationModeInternal(uint16_t aAnimationMode); michael@0: michael@0: /** michael@0: * Decides whether animation should or should not be happening, michael@0: * and makes sure the right thing is being done. michael@0: */ michael@0: virtual void EvaluateAnimation(); michael@0: michael@0: /** michael@0: * Extended by child classes, if they have additional michael@0: * conditions for being able to animate. michael@0: */ michael@0: virtual bool ShouldAnimate() { michael@0: return mAnimationConsumers > 0 && mAnimationMode != kDontAnimMode; michael@0: } michael@0: michael@0: virtual nsresult StartAnimation() = 0; michael@0: virtual nsresult StopAnimation() = 0; michael@0: michael@0: // Member data shared by all implementations of this abstract class michael@0: nsRefPtr mStatusTracker; michael@0: nsRefPtr mURI; michael@0: uint64_t mInnerWindowId; michael@0: uint32_t mAnimationConsumers; michael@0: uint16_t mAnimationMode; // Enum values in imgIContainer michael@0: bool mInitialized:1; // Have we been initalized? michael@0: bool mAnimating:1; // Are we currently animating? michael@0: bool mError:1; // Error handling michael@0: }; michael@0: michael@0: } // namespace image michael@0: } // namespace mozilla michael@0: michael@0: #endif // MOZILLA_IMAGELIB_IMAGE_H_