2010-08-14 08:09:48 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2010-08-14 08:09:48 +04:00
|
|
|
|
2015-05-15 06:52:05 +03:00
|
|
|
#ifndef mozilla_image_Image_h
|
|
|
|
#define mozilla_image_Image_h
|
2010-08-14 08:09:48 +04:00
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2015-01-07 12:35:20 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2015-07-29 07:02:45 +03:00
|
|
|
#include "gfx2DGlue.h"
|
2010-08-14 08:09:48 +04:00
|
|
|
#include "imgIContainer.h"
|
2013-09-28 22:28:42 +04:00
|
|
|
#include "ImageURL.h"
|
2014-05-08 13:53:00 +04:00
|
|
|
#include "nsStringFwd.h"
|
2015-04-28 21:46:17 +03:00
|
|
|
#include "ProgressTracker.h"
|
|
|
|
#include "SurfaceCache.h"
|
2013-09-07 17:01:08 +04:00
|
|
|
|
|
|
|
class nsIRequest;
|
|
|
|
class nsIInputStream;
|
2010-08-14 08:09:48 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-01-06 20:02:27 +04:00
|
|
|
namespace image {
|
2010-08-14 08:09:48 +04:00
|
|
|
|
2015-04-28 21:46:17 +03:00
|
|
|
class Image;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Memory Reporting
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct MemoryCounter
|
|
|
|
{
|
|
|
|
MemoryCounter()
|
|
|
|
: mSource(0)
|
|
|
|
, mDecodedHeap(0)
|
|
|
|
, mDecodedNonHeap(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void SetSource(size_t aCount) { mSource = aCount; }
|
|
|
|
size_t Source() const { return mSource; }
|
|
|
|
void SetDecodedHeap(size_t aCount) { mDecodedHeap = aCount; }
|
|
|
|
size_t DecodedHeap() const { return mDecodedHeap; }
|
|
|
|
void SetDecodedNonHeap(size_t aCount) { mDecodedNonHeap = aCount; }
|
|
|
|
size_t DecodedNonHeap() const { return mDecodedNonHeap; }
|
|
|
|
|
|
|
|
MemoryCounter& operator+=(const MemoryCounter& aOther)
|
|
|
|
{
|
|
|
|
mSource += aOther.mSource;
|
|
|
|
mDecodedHeap += aOther.mDecodedHeap;
|
|
|
|
mDecodedNonHeap += aOther.mDecodedNonHeap;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t mSource;
|
|
|
|
size_t mDecodedHeap;
|
|
|
|
size_t mDecodedNonHeap;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class SurfaceMemoryCounterType
|
|
|
|
{
|
|
|
|
NORMAL,
|
|
|
|
COMPOSITING,
|
|
|
|
COMPOSITING_PREV
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SurfaceMemoryCounter
|
|
|
|
{
|
|
|
|
SurfaceMemoryCounter(const SurfaceKey& aKey,
|
|
|
|
bool aIsLocked,
|
|
|
|
SurfaceMemoryCounterType aType =
|
|
|
|
SurfaceMemoryCounterType::NORMAL)
|
|
|
|
: mKey(aKey)
|
|
|
|
, mType(aType)
|
|
|
|
, mIsLocked(aIsLocked)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
const SurfaceKey& Key() const { return mKey; }
|
|
|
|
MemoryCounter& Values() { return mValues; }
|
|
|
|
const MemoryCounter& Values() const { return mValues; }
|
|
|
|
SurfaceMemoryCounterType Type() const { return mType; }
|
|
|
|
bool IsLocked() const { return mIsLocked; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const SurfaceKey mKey;
|
|
|
|
MemoryCounter mValues;
|
|
|
|
const SurfaceMemoryCounterType mType;
|
|
|
|
const bool mIsLocked;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ImageMemoryCounter
|
|
|
|
{
|
|
|
|
ImageMemoryCounter(Image* aImage,
|
|
|
|
MallocSizeOf aMallocSizeOf,
|
|
|
|
bool aIsUsed);
|
|
|
|
|
|
|
|
nsCString& URI() { return mURI; }
|
|
|
|
const nsCString& URI() const { return mURI; }
|
|
|
|
const nsTArray<SurfaceMemoryCounter>& Surfaces() const { return mSurfaces; }
|
|
|
|
const gfx::IntSize IntrinsicSize() const { return mIntrinsicSize; }
|
|
|
|
const MemoryCounter& Values() const { return mValues; }
|
|
|
|
uint16_t Type() const { return mType; }
|
|
|
|
bool IsUsed() const { return mIsUsed; }
|
|
|
|
|
|
|
|
bool IsNotable() const
|
|
|
|
{
|
|
|
|
const size_t NotableThreshold = 16 * 1024;
|
|
|
|
size_t total = mValues.Source() + mValues.DecodedHeap()
|
|
|
|
+ mValues.DecodedNonHeap();
|
|
|
|
return total >= NotableThreshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCString mURI;
|
|
|
|
nsTArray<SurfaceMemoryCounter> mSurfaces;
|
|
|
|
gfx::IntSize mIntrinsicSize;
|
|
|
|
MemoryCounter mValues;
|
|
|
|
uint16_t mType;
|
|
|
|
const bool mIsUsed;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Image Base Types
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-08-14 08:09:48 +04:00
|
|
|
class Image : public imgIContainer
|
|
|
|
{
|
|
|
|
public:
|
2010-08-14 08:09:49 +04:00
|
|
|
/**
|
|
|
|
* Flags for Image initialization.
|
|
|
|
*
|
|
|
|
* Meanings:
|
|
|
|
*
|
|
|
|
* INIT_FLAG_NONE: Lack of flags
|
|
|
|
*
|
|
|
|
* INIT_FLAG_DISCARDABLE: The container should be discardable
|
|
|
|
*
|
2015-04-08 02:44:29 +03:00
|
|
|
* INIT_FLAG_DECODE_IMMEDIATELY: The container should decode as soon as
|
|
|
|
* possible, regardless of what our heuristics say.
|
|
|
|
*
|
2015-01-07 12:37:20 +03:00
|
|
|
* INIT_FLAG_TRANSIENT: The container is likely to exist for only a short time
|
|
|
|
* before being destroyed. (For example, containers for
|
|
|
|
* multipart/x-mixed-replace image parts fall into this category.) If this
|
2015-03-25 01:39:00 +03:00
|
|
|
* flag is set, INIT_FLAG_DISCARDABLE and INIT_FLAG_DECODE_ONLY_ON_DRAW must
|
|
|
|
* not be set.
|
2015-01-19 01:02:13 +03:00
|
|
|
*
|
2015-06-30 12:37:58 +03:00
|
|
|
* INIT_FLAG_SYNC_LOAD: The container is being loaded synchronously, so
|
|
|
|
* it should avoid relying on async workers to get the container ready.
|
2010-08-14 08:09:49 +04:00
|
|
|
*/
|
2015-01-19 01:02:13 +03:00
|
|
|
static const uint32_t INIT_FLAG_NONE = 0x0;
|
|
|
|
static const uint32_t INIT_FLAG_DISCARDABLE = 0x1;
|
2015-07-15 04:16:31 +03:00
|
|
|
static const uint32_t INIT_FLAG_DECODE_IMMEDIATELY = 0x2;
|
|
|
|
static const uint32_t INIT_FLAG_TRANSIENT = 0x4;
|
2015-09-20 02:21:05 +03:00
|
|
|
static const uint32_t INIT_FLAG_SYNC_LOAD = 0x8;
|
2010-08-14 08:09:49 +04:00
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
virtual already_AddRefed<ProgressTracker> GetProgressTracker() = 0;
|
|
|
|
virtual void SetProgressTracker(ProgressTracker* aProgressTracker) {}
|
2012-12-20 02:24:32 +04:00
|
|
|
|
2010-08-14 08:09:49 +04:00
|
|
|
/**
|
2014-11-27 05:00:15 +03:00
|
|
|
* The size, in bytes, occupied by the compressed source data of the image.
|
|
|
|
* If MallocSizeOf does not work on this platform, uses a fallback approach to
|
|
|
|
* ensure that something reasonable is always returned.
|
2010-08-14 08:09:49 +04:00
|
|
|
*/
|
2015-04-28 21:46:17 +03:00
|
|
|
virtual size_t
|
|
|
|
SizeOfSourceWithComputedFallback(MallocSizeOf aMallocSizeOf) const = 0;
|
2010-10-06 19:37:12 +04:00
|
|
|
|
|
|
|
/**
|
2015-04-28 21:46:17 +03:00
|
|
|
* Collect an accounting of the memory occupied by the image's surfaces (which
|
|
|
|
* together make up its decoded data). Each surface is recorded as a separate
|
|
|
|
* SurfaceMemoryCounter, stored in @aCounters.
|
2013-03-30 00:14:19 +04:00
|
|
|
*/
|
2015-04-28 21:46:17 +03:00
|
|
|
virtual void CollectSizeOfSurfaces(nsTArray<SurfaceMemoryCounter>& aCounters,
|
|
|
|
MallocSizeOf aMallocSizeOf) const = 0;
|
2014-05-08 13:53:00 +04:00
|
|
|
|
2012-12-20 02:24:32 +04:00
|
|
|
virtual void IncrementAnimationConsumers() = 0;
|
|
|
|
virtual void DecrementAnimationConsumers() = 0;
|
2010-09-08 04:33:02 +04:00
|
|
|
#ifdef DEBUG
|
2012-12-20 02:24:32 +04:00
|
|
|
virtual uint32_t GetAnimationConsumers() = 0;
|
2010-09-08 04:33:02 +04:00
|
|
|
#endif
|
|
|
|
|
2012-12-18 02:05:18 +04:00
|
|
|
/**
|
|
|
|
* Called from OnDataAvailable when the stream associated with the image has
|
|
|
|
* received new image data. The arguments are the same as OnDataAvailable's,
|
|
|
|
* but by separating this functionality into a different method we don't
|
|
|
|
* interfere with subclasses which wish to implement nsIStreamListener.
|
|
|
|
*
|
|
|
|
* Images should not do anything that could send out notifications until they
|
|
|
|
* have received their first OnImageDataAvailable notification; in
|
|
|
|
* particular, this means that instantiating decoders should be deferred
|
|
|
|
* until OnImageDataAvailable is called.
|
|
|
|
*/
|
|
|
|
virtual nsresult OnImageDataAvailable(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsIInputStream* aInStr,
|
|
|
|
uint64_t aSourceOffset,
|
|
|
|
uint32_t aCount) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called from OnStopRequest when the image's underlying request completes.
|
2013-02-14 06:41:10 +04:00
|
|
|
*
|
|
|
|
* @param aRequest The completed request.
|
|
|
|
* @param aContext Context from Necko's OnStopRequest.
|
|
|
|
* @param aStatus A success or failure code.
|
|
|
|
* @param aLastPart Whether this is the final part of the underlying request.
|
2012-12-18 02:05:18 +04:00
|
|
|
*/
|
|
|
|
virtual nsresult OnImageDataComplete(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
2013-02-14 06:41:10 +04:00
|
|
|
nsresult aStatus,
|
|
|
|
bool aLastPart) = 0;
|
2012-12-18 02:05:18 +04:00
|
|
|
|
2014-11-28 06:55:57 +03:00
|
|
|
/**
|
2015-09-20 02:20:59 +03:00
|
|
|
* Called when the SurfaceCache discards a surface belonging to this image.
|
2014-11-28 06:55:57 +03:00
|
|
|
*/
|
|
|
|
virtual void OnSurfaceDiscarded() = 0;
|
|
|
|
|
2012-12-20 02:24:32 +04:00
|
|
|
virtual void SetInnerWindowID(uint64_t aInnerWindowId) = 0;
|
|
|
|
virtual uint64_t InnerWindowID() const = 0;
|
|
|
|
|
|
|
|
virtual bool HasError() = 0;
|
|
|
|
virtual void SetHasError() = 0;
|
|
|
|
|
2013-09-28 22:28:42 +04:00
|
|
|
virtual ImageURL* GetURI() = 0;
|
2015-03-17 22:25:35 +03:00
|
|
|
|
|
|
|
virtual void ReportUseCounters() { }
|
2012-12-20 02:24:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class ImageResource : public Image
|
|
|
|
{
|
|
|
|
public:
|
2015-03-21 19:28:04 +03:00
|
|
|
already_AddRefed<ProgressTracker> GetProgressTracker() override
|
2014-12-04 22:23:00 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> progressTracker = mProgressTracker;
|
2014-11-15 07:10:47 +03:00
|
|
|
MOZ_ASSERT(progressTracker);
|
|
|
|
return progressTracker.forget();
|
2013-09-28 22:28:44 +04:00
|
|
|
}
|
2014-12-04 22:23:00 +03:00
|
|
|
|
|
|
|
void SetProgressTracker(
|
2015-03-21 19:28:04 +03:00
|
|
|
ProgressTracker* aProgressTracker) override final
|
2014-12-04 22:23:00 +03:00
|
|
|
{
|
2014-11-15 07:10:47 +03:00
|
|
|
MOZ_ASSERT(aProgressTracker);
|
|
|
|
MOZ_ASSERT(!mProgressTracker);
|
|
|
|
mProgressTracker = aProgressTracker;
|
2013-09-28 22:28:44 +04:00
|
|
|
}
|
2012-12-20 02:24:32 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void IncrementAnimationConsumers() override;
|
|
|
|
virtual void DecrementAnimationConsumers() override;
|
2012-12-20 02:24:32 +04:00
|
|
|
#ifdef DEBUG
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual uint32_t GetAnimationConsumers() override
|
2014-12-04 22:23:00 +03:00
|
|
|
{
|
|
|
|
return mAnimationConsumers;
|
|
|
|
}
|
2012-12-20 02:24:32 +04:00
|
|
|
#endif
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void OnSurfaceDiscarded() override { }
|
2014-11-28 06:55:57 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void SetInnerWindowID(uint64_t aInnerWindowId) override
|
2014-12-04 22:23:00 +03:00
|
|
|
{
|
2011-08-25 00:44:35 +04:00
|
|
|
mInnerWindowId = aInnerWindowId;
|
2010-12-20 19:21:59 +03:00
|
|
|
}
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual uint64_t InnerWindowID() const override { return mInnerWindowId; }
|
2010-12-20 19:21:59 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool HasError() override { return mError; }
|
|
|
|
virtual void SetHasError() override { mError = true; }
|
2012-12-15 03:42:18 +04:00
|
|
|
|
2012-12-20 01:28:54 +04:00
|
|
|
/*
|
|
|
|
* Returns a non-AddRefed pointer to the URI associated with this image.
|
2013-09-12 04:01:59 +04:00
|
|
|
* Illegal to use off-main-thread.
|
2012-12-20 01:28:54 +04:00
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual ImageURL* GetURI() override { return mURI.get(); }
|
2012-12-20 01:28:54 +04:00
|
|
|
|
2010-08-14 08:09:48 +04:00
|
|
|
protected:
|
2014-09-02 20:20:24 +04:00
|
|
|
explicit ImageResource(ImageURL* aURI);
|
2015-04-29 02:32:02 +03:00
|
|
|
~ImageResource();
|
2010-08-14 08:09:48 +04:00
|
|
|
|
2012-10-20 00:27:11 +04:00
|
|
|
// Shared functionality for implementors of imgIContainer. Every
|
|
|
|
// implementation of attribute animationMode should forward here.
|
2014-12-04 22:23:00 +03:00
|
|
|
nsresult GetAnimationModeInternal(uint16_t* aAnimationMode);
|
2012-10-20 00:27:11 +04:00
|
|
|
nsresult SetAnimationModeInternal(uint16_t aAnimationMode);
|
|
|
|
|
2014-05-08 22:37:39 +04:00
|
|
|
/**
|
|
|
|
* Helper for RequestRefresh.
|
|
|
|
*
|
|
|
|
* If we've had a "recent" refresh (i.e. if this image is being used in
|
|
|
|
* multiple documents & some other document *just* called RequestRefresh() on
|
|
|
|
* this image with a timestamp close to aTime), this method returns true.
|
|
|
|
*
|
|
|
|
* Otherwise, this method updates mLastRefreshTime to aTime & returns false.
|
|
|
|
*/
|
2014-07-10 19:00:31 +04:00
|
|
|
bool HadRecentRefresh(const TimeStamp& aTime);
|
2014-05-08 22:37:39 +04:00
|
|
|
|
2010-09-08 04:33:02 +04:00
|
|
|
/**
|
|
|
|
* Decides whether animation should or should not be happening,
|
|
|
|
* and makes sure the right thing is being done.
|
|
|
|
*/
|
|
|
|
virtual void EvaluateAnimation();
|
|
|
|
|
2012-12-20 02:24:32 +04:00
|
|
|
/**
|
|
|
|
* Extended by child classes, if they have additional
|
|
|
|
* conditions for being able to animate.
|
|
|
|
*/
|
|
|
|
virtual bool ShouldAnimate() {
|
|
|
|
return mAnimationConsumers > 0 && mAnimationMode != kDontAnimMode;
|
|
|
|
}
|
|
|
|
|
2010-09-08 04:33:02 +04:00
|
|
|
virtual nsresult StartAnimation() = 0;
|
|
|
|
virtual nsresult StopAnimation() = 0;
|
|
|
|
|
2010-08-14 08:09:48 +04:00
|
|
|
// Member data shared by all implementations of this abstract class
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> mProgressTracker;
|
|
|
|
RefPtr<ImageURL> mURI;
|
2014-05-08 22:37:39 +04:00
|
|
|
TimeStamp mLastRefreshTime;
|
2013-09-12 04:01:59 +04:00
|
|
|
uint64_t mInnerWindowId;
|
|
|
|
uint32_t mAnimationConsumers;
|
|
|
|
uint16_t mAnimationMode; // Enum values in imgIContainer
|
|
|
|
bool mInitialized:1; // Have we been initalized?
|
|
|
|
bool mAnimating:1; // Are we currently animating?
|
|
|
|
bool mError:1; // Error handling
|
2010-08-14 08:09:48 +04:00
|
|
|
};
|
|
|
|
|
2012-01-06 20:02:27 +04:00
|
|
|
} // namespace image
|
2010-08-14 08:09:48 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-05-15 06:52:05 +03:00
|
|
|
#endif // mozilla_image_Image_h
|