gecko-dev/image/ProgressTracker.h

256 строки
9.0 KiB
C
Исходник Обычный вид История

/* -*- 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/. */
Bug 1038536 - Flatten image/src/ directory. r=seth --HG-- rename : image/src/BMPFileHeaders.h => image/BMPFileHeaders.h rename : image/src/ClippedImage.cpp => image/ClippedImage.cpp rename : image/src/ClippedImage.h => image/ClippedImage.h rename : image/src/DecodePool.cpp => image/DecodePool.cpp rename : image/src/DecodePool.h => image/DecodePool.h rename : image/src/Decoder.cpp => image/Decoder.cpp rename : image/src/Decoder.h => image/Decoder.h rename : image/src/Downscaler.cpp => image/Downscaler.cpp rename : image/src/Downscaler.h => image/Downscaler.h rename : image/src/DynamicImage.cpp => image/DynamicImage.cpp rename : image/src/DynamicImage.h => image/DynamicImage.h rename : image/src/FrameAnimator.cpp => image/FrameAnimator.cpp rename : image/src/FrameAnimator.h => image/FrameAnimator.h rename : image/src/FrozenImage.cpp => image/FrozenImage.cpp rename : image/src/FrozenImage.h => image/FrozenImage.h rename : image/src/ICOFileHeaders.h => image/ICOFileHeaders.h rename : image/src/IProgressObserver.h => image/IProgressObserver.h rename : image/src/Image.cpp => image/Image.cpp rename : image/src/Image.h => image/Image.h rename : image/src/ImageFactory.cpp => image/ImageFactory.cpp rename : image/src/ImageFactory.h => image/ImageFactory.h rename : image/src/ImageMetadata.cpp => image/ImageMetadata.cpp rename : image/src/ImageMetadata.h => image/ImageMetadata.h rename : image/src/ImageOps.cpp => image/ImageOps.cpp rename : image/src/ImageOps.h => image/ImageOps.h rename : image/src/ImageRegion.h => image/ImageRegion.h rename : image/src/ImageURL.h => image/ImageURL.h rename : image/src/ImageWrapper.cpp => image/ImageWrapper.cpp rename : image/src/ImageWrapper.h => image/ImageWrapper.h rename : image/src/MultipartImage.cpp => image/MultipartImage.cpp rename : image/src/MultipartImage.h => image/MultipartImage.h rename : image/src/Orientation.h => image/Orientation.h rename : image/src/OrientedImage.cpp => image/OrientedImage.cpp rename : image/src/OrientedImage.h => image/OrientedImage.h rename : image/src/ProgressTracker.cpp => image/ProgressTracker.cpp rename : image/src/ProgressTracker.h => image/ProgressTracker.h rename : image/src/RasterImage.cpp => image/RasterImage.cpp rename : image/src/RasterImage.h => image/RasterImage.h rename : image/src/SVGDocumentWrapper.cpp => image/SVGDocumentWrapper.cpp rename : image/src/SVGDocumentWrapper.h => image/SVGDocumentWrapper.h rename : image/src/ScriptedNotificationObserver.cpp => image/ScriptedNotificationObserver.cpp rename : image/src/ScriptedNotificationObserver.h => image/ScriptedNotificationObserver.h rename : image/src/ShutdownTracker.cpp => image/ShutdownTracker.cpp rename : image/src/ShutdownTracker.h => image/ShutdownTracker.h rename : image/src/SourceBuffer.cpp => image/SourceBuffer.cpp rename : image/src/SourceBuffer.h => image/SourceBuffer.h rename : image/src/SurfaceCache.cpp => image/SurfaceCache.cpp rename : image/src/SurfaceCache.h => image/SurfaceCache.h rename : image/src/VectorImage.cpp => image/VectorImage.cpp rename : image/src/VectorImage.h => image/VectorImage.h rename : image/src/imgFrame.cpp => image/imgFrame.cpp rename : image/src/imgFrame.h => image/imgFrame.h rename : image/src/imgLoader.cpp => image/imgLoader.cpp rename : image/src/imgLoader.h => image/imgLoader.h rename : image/src/imgRequest.cpp => image/imgRequest.cpp rename : image/src/imgRequest.h => image/imgRequest.h rename : image/src/imgRequestProxy.cpp => image/imgRequestProxy.cpp rename : image/src/imgRequestProxy.h => image/imgRequestProxy.h rename : image/src/imgTools.cpp => image/imgTools.cpp rename : image/src/imgTools.h => image/imgTools.h
2015-05-15 06:52:05 +03:00
#ifndef mozilla_image_ProgressTracker_h
#define mozilla_image_ProgressTracker_h
#include "CopyOnWrite.h"
#include "mozilla/NotNull.h"
#include "mozilla/Mutex.h"
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
#include "mozilla/RefPtr.h"
#include "mozilla/WeakPtr.h"
#include "nsDataHashtable.h"
#include "nsCOMPtr.h"
#include "nsTObserverArray.h"
#include "nsThreadUtils.h"
#include "nsRect.h"
#include "IProgressObserver.h"
class nsIRunnable;
namespace mozilla {
namespace image {
class AsyncNotifyRunnable;
class AsyncNotifyCurrentStateRunnable;
class Image;
/**
* Image progress bitflags.
*
* See CheckProgressConsistency() for the invariants we enforce about the
* ordering dependencies betweeen these flags.
*/
enum {
FLAG_SIZE_AVAILABLE = 1u << 0, // STATUS_SIZE_AVAILABLE
FLAG_DECODE_COMPLETE = 1u << 1, // STATUS_DECODE_COMPLETE
FLAG_FRAME_COMPLETE = 1u << 2, // STATUS_FRAME_COMPLETE
FLAG_LOAD_COMPLETE = 1u << 3, // STATUS_LOAD_COMPLETE
FLAG_IS_ANIMATED = 1u << 6, // STATUS_IS_ANIMATED
FLAG_HAS_TRANSPARENCY = 1u << 7, // STATUS_HAS_TRANSPARENCY
FLAG_LAST_PART_COMPLETE = 1u << 8,
FLAG_HAS_ERROR = 1u << 9 // STATUS_ERROR
};
typedef uint32_t Progress;
const uint32_t NoProgress = 0;
inline Progress LoadCompleteProgress(bool aLastPart,
bool aError,
nsresult aStatus)
{
Progress progress = FLAG_LOAD_COMPLETE;
if (aLastPart) {
progress |= FLAG_LAST_PART_COMPLETE;
}
if (NS_FAILED(aStatus) || aError) {
progress |= FLAG_HAS_ERROR;
}
return progress;
}
/**
* ProgressTracker stores its observers in an ObserverTable, which is a hash
* table mapping raw pointers to WeakPtr's to the same objects. This sounds like
* unnecessary duplication of information, but it's necessary for stable hash
* values since WeakPtr's lose the knowledge of which object they used to point
* to when that object is destroyed.
*
* ObserverTable subclasses nsDataHashtable to add reference counting support
* and a copy constructor, both of which are needed for use with CopyOnWrite<T>.
*/
class ObserverTable
: public nsDataHashtable<nsPtrHashKey<IProgressObserver>,
WeakPtr<IProgressObserver>>
{
public:
NS_INLINE_DECL_REFCOUNTING(ObserverTable);
ObserverTable() = default;
ObserverTable(const ObserverTable& aOther)
{
NS_WARNING("Forced to copy ObserverTable due to nested notifications");
for (auto iter = aOther.ConstIter(); !iter.Done(); iter.Next()) {
this->Put(iter.Key(), iter.Data());
}
}
private:
~ObserverTable() { }
};
/**
* ProgressTracker is a class that records an Image's progress through the
* loading and decoding process, and makes it possible to send notifications to
* IProgressObservers, both synchronously and asynchronously.
*
* When a new observer needs to be notified of the current progress of an image,
* call the Notify() method on this class with the relevant observer as its
* argument, and the notifications will be replayed to the observer
* asynchronously.
*/
class ProgressTracker : public mozilla::SupportsWeakPtr<ProgressTracker>
{
virtual ~ProgressTracker() { }
public:
MOZ_DECLARE_WEAKREFERENCE_TYPENAME(ProgressTracker)
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ProgressTracker)
ProgressTracker();
bool HasImage() const { MutexAutoLock lock(mMutex); return mImage; }
already_AddRefed<Image> GetImage() const
{
MutexAutoLock lock(mMutex);
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<Image> image = mImage;
return image.forget();
}
// Get the current image status (as in imgIRequest).
uint32_t GetImageStatus() const;
// Get the current Progress.
Progress GetProgress() const { return mProgress; }
// Schedule an asynchronous "replaying" of all the notifications that would
// have to happen to put us in the current state.
// We will also take note of any notifications that happen between the time
// Notify() is called and when we call SyncNotify on |aObserver|, and replay
// them as well.
// Should be called on the main thread only, since observers and GetURI are
// not threadsafe.
void Notify(IProgressObserver* aObserver);
// Schedule an asynchronous "replaying" of all the notifications that would
// have to happen to put us in the state we are in right now.
// Unlike Notify(), does *not* take into account future notifications.
// This is only useful if you do not have an imgRequest, e.g., if you are a
// static request returned from imgIRequest::GetStaticRequest().
// Should be called on the main thread only, since observers and GetURI are
// not threadsafe.
void NotifyCurrentState(IProgressObserver* aObserver);
// "Replay" all of the notifications that would have to happen to put us in
// the state we're currently in.
// Only use this if you're already servicing an asynchronous call (e.g.
// OnStartRequest).
// Should be called on the main thread only, since observers and GetURI are
// not threadsafe.
void SyncNotify(IProgressObserver* aObserver);
// Get this ProgressTracker ready for a new request. This resets all the
// state that doesn't persist between requests.
void ResetForNewRequest();
// Stateless notifications. These are dispatched and immediately forgotten
// about. All of these notifications are main thread only.
void OnDiscard();
void OnUnlockedDraw();
void OnImageAvailable();
// Compute the difference between this our progress and aProgress. This allows
// callers to predict whether SyncNotifyProgress will send any notifications.
Progress Difference(Progress aProgress) const
{
return ~mProgress & aProgress;
}
// Update our state to incorporate the changes in aProgress and synchronously
// notify our observers.
//
// Because this may result in recursive notifications, no decoding locks may
// be held. Called on the main thread only.
void SyncNotifyProgress(Progress aProgress,
const nsIntRect& aInvalidRect = nsIntRect());
// We manage a set of observers that are using an image and thus concerned
// with its loading progress. Weak pointers.
void AddObserver(IProgressObserver* aObserver);
bool RemoveObserver(IProgressObserver* aObserver);
uint32_t ObserverCount() const;
// Get the event target we should currently dispatch events to.
already_AddRefed<nsIEventTarget> GetEventTarget() const;
// Resets our weak reference to our image. Image subclasses should call this
// in their destructor.
void ResetImage();
Bug 1321946. Adjust asserts about the progress of an image to deal with how multipart images are handled. r=aosmond For multipart images we create a MultipartImage which contains each part. Each part in turn is a VectorImage or RasterImage. The MultipartImage and each part image all have their own ProgressTracker. The ProgressTracker for the MultipartImage observes the notifications of each part image via the IProgressObserver interface. This interfaces notably has no way to notify about an image error. So when a part image has an error it never gets propagated to the MultipartImage's ProgressTracker. This confuses our assertions about consistency of progress notifications. In this case we expect that when we get the load complete notification then we either have the size of the image or we encountered an error. So if the first part of a multipart image is broken and we are unable to get a size from it we will trigger this assertion. There are two ways to fix this. One would be to propagate errors to the MultipartImage's ProgressTracker. This would put the ProgressTracker for the MultipartImage permanently into error state and prevent showing the images from the remaining parts if one part image had an error. So in this patch I create a way to tell a ProgressTracker that is is for a multipart image, and use that to relax the assertions. As far as I can tell our code should be able to handle "ignoring" an error in a bad part image. Addtionaly there is a way that an error flag can get propagated to the MultipartImage's tracker: in MultipartImage::FinishTransition we get the progress directly from the part image and notify for it. This seems like an oversight as the comment at https://dxr.mozilla.org/mozilla-central/rev/bfa85d23df57c8a1db17c99b267667becc1c4afd/image/imgRequest.cpp#989 indicates that we don't want one bad part to prevent later parts from displaying. So we add the error flag to the ones we filter out when we propagate progress.
2016-12-04 01:07:10 +03:00
// Tell this progress tracker that it is for a multipart image.
void SetIsMultipart() { mIsMultipart = true; }
private:
friend class AsyncNotifyRunnable;
friend class AsyncNotifyCurrentStateRunnable;
friend class ImageFactory;
ProgressTracker(const ProgressTracker& aOther) = delete;
// Sets our weak reference to our image. Only ImageFactory should call this.
void SetImage(Image* aImage);
// Send some notifications that would be necessary to make |aObserver| believe
// the request is finished downloading and decoding. We only send
// FLAG_LOAD_COMPLETE and FLAG_ONLOAD_UNBLOCKED, and only if necessary.
void EmulateRequestFinished(IProgressObserver* aObserver);
// Main thread only because it deals with the observer service.
void FireFailureNotification();
// The runnable, if any, that we've scheduled to deliver async notifications.
nsCOMPtr<nsIRunnable> mRunnable;
// mMutex protects access to mImage and mEventTarget.
mutable Mutex mMutex;
// mImage is a weak ref; it should be set to null when the image goes out of
// scope.
Image* mImage;
// mEventTarget is the current, best effort event target to dispatch
// notifications to from the decoder threads. It will change as observers are
// added and removed (see mObserversWithTargets).
NotNull<nsCOMPtr<nsIEventTarget>> mEventTarget;
// How many observers have been added that have an explicit event target.
// When the first observer is added with an explicit event target, we will
// default to that as long as all observers use the same target. If a new
// observer is added which has a different event target, we will switch to
// using the unlabeled main thread event target which is safe for all
// observers. If all observers with explicit event targets are removed, we
// will revert back to the initial event target (for SystemGroup). An
// observer without an explicit event target does not care what context it
// is dispatched in, and thus does not impact the state.
uint32_t mObserversWithTargets;
// Hashtable of observers attached to the image. Each observer represents a
// consumer using the image. Main thread only.
CopyOnWrite<ObserverTable> mObservers;
Progress mProgress;
Bug 1321946. Adjust asserts about the progress of an image to deal with how multipart images are handled. r=aosmond For multipart images we create a MultipartImage which contains each part. Each part in turn is a VectorImage or RasterImage. The MultipartImage and each part image all have their own ProgressTracker. The ProgressTracker for the MultipartImage observes the notifications of each part image via the IProgressObserver interface. This interfaces notably has no way to notify about an image error. So when a part image has an error it never gets propagated to the MultipartImage's ProgressTracker. This confuses our assertions about consistency of progress notifications. In this case we expect that when we get the load complete notification then we either have the size of the image or we encountered an error. So if the first part of a multipart image is broken and we are unable to get a size from it we will trigger this assertion. There are two ways to fix this. One would be to propagate errors to the MultipartImage's ProgressTracker. This would put the ProgressTracker for the MultipartImage permanently into error state and prevent showing the images from the remaining parts if one part image had an error. So in this patch I create a way to tell a ProgressTracker that is is for a multipart image, and use that to relax the assertions. As far as I can tell our code should be able to handle "ignoring" an error in a bad part image. Addtionaly there is a way that an error flag can get propagated to the MultipartImage's tracker: in MultipartImage::FinishTransition we get the progress directly from the part image and notify for it. This seems like an oversight as the comment at https://dxr.mozilla.org/mozilla-central/rev/bfa85d23df57c8a1db17c99b267667becc1c4afd/image/imgRequest.cpp#989 indicates that we don't want one bad part to prevent later parts from displaying. So we add the error flag to the ones we filter out when we propagate progress.
2016-12-04 01:07:10 +03:00
// Whether this is a progress tracker for a multipart image.
bool mIsMultipart;
};
} // namespace image
} // namespace mozilla
Bug 1038536 - Flatten image/src/ directory. r=seth --HG-- rename : image/src/BMPFileHeaders.h => image/BMPFileHeaders.h rename : image/src/ClippedImage.cpp => image/ClippedImage.cpp rename : image/src/ClippedImage.h => image/ClippedImage.h rename : image/src/DecodePool.cpp => image/DecodePool.cpp rename : image/src/DecodePool.h => image/DecodePool.h rename : image/src/Decoder.cpp => image/Decoder.cpp rename : image/src/Decoder.h => image/Decoder.h rename : image/src/Downscaler.cpp => image/Downscaler.cpp rename : image/src/Downscaler.h => image/Downscaler.h rename : image/src/DynamicImage.cpp => image/DynamicImage.cpp rename : image/src/DynamicImage.h => image/DynamicImage.h rename : image/src/FrameAnimator.cpp => image/FrameAnimator.cpp rename : image/src/FrameAnimator.h => image/FrameAnimator.h rename : image/src/FrozenImage.cpp => image/FrozenImage.cpp rename : image/src/FrozenImage.h => image/FrozenImage.h rename : image/src/ICOFileHeaders.h => image/ICOFileHeaders.h rename : image/src/IProgressObserver.h => image/IProgressObserver.h rename : image/src/Image.cpp => image/Image.cpp rename : image/src/Image.h => image/Image.h rename : image/src/ImageFactory.cpp => image/ImageFactory.cpp rename : image/src/ImageFactory.h => image/ImageFactory.h rename : image/src/ImageMetadata.cpp => image/ImageMetadata.cpp rename : image/src/ImageMetadata.h => image/ImageMetadata.h rename : image/src/ImageOps.cpp => image/ImageOps.cpp rename : image/src/ImageOps.h => image/ImageOps.h rename : image/src/ImageRegion.h => image/ImageRegion.h rename : image/src/ImageURL.h => image/ImageURL.h rename : image/src/ImageWrapper.cpp => image/ImageWrapper.cpp rename : image/src/ImageWrapper.h => image/ImageWrapper.h rename : image/src/MultipartImage.cpp => image/MultipartImage.cpp rename : image/src/MultipartImage.h => image/MultipartImage.h rename : image/src/Orientation.h => image/Orientation.h rename : image/src/OrientedImage.cpp => image/OrientedImage.cpp rename : image/src/OrientedImage.h => image/OrientedImage.h rename : image/src/ProgressTracker.cpp => image/ProgressTracker.cpp rename : image/src/ProgressTracker.h => image/ProgressTracker.h rename : image/src/RasterImage.cpp => image/RasterImage.cpp rename : image/src/RasterImage.h => image/RasterImage.h rename : image/src/SVGDocumentWrapper.cpp => image/SVGDocumentWrapper.cpp rename : image/src/SVGDocumentWrapper.h => image/SVGDocumentWrapper.h rename : image/src/ScriptedNotificationObserver.cpp => image/ScriptedNotificationObserver.cpp rename : image/src/ScriptedNotificationObserver.h => image/ScriptedNotificationObserver.h rename : image/src/ShutdownTracker.cpp => image/ShutdownTracker.cpp rename : image/src/ShutdownTracker.h => image/ShutdownTracker.h rename : image/src/SourceBuffer.cpp => image/SourceBuffer.cpp rename : image/src/SourceBuffer.h => image/SourceBuffer.h rename : image/src/SurfaceCache.cpp => image/SurfaceCache.cpp rename : image/src/SurfaceCache.h => image/SurfaceCache.h rename : image/src/VectorImage.cpp => image/VectorImage.cpp rename : image/src/VectorImage.h => image/VectorImage.h rename : image/src/imgFrame.cpp => image/imgFrame.cpp rename : image/src/imgFrame.h => image/imgFrame.h rename : image/src/imgLoader.cpp => image/imgLoader.cpp rename : image/src/imgLoader.h => image/imgLoader.h rename : image/src/imgRequest.cpp => image/imgRequest.cpp rename : image/src/imgRequest.h => image/imgRequest.h rename : image/src/imgRequestProxy.cpp => image/imgRequestProxy.cpp rename : image/src/imgRequestProxy.h => image/imgRequestProxy.h rename : image/src/imgTools.cpp => image/imgTools.cpp rename : image/src/imgTools.h => image/imgTools.h
2015-05-15 06:52:05 +03:00
#endif // mozilla_image_ProgressTracker_h