1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /* table of images used in a document, for batch locking/unlocking and
8  * animating */
9 
10 #ifndef mozilla_dom_ImageTracker
11 #define mozilla_dom_ImageTracker
12 
13 #include "nsDataHashtable.h"
14 #include "nsHashKeys.h"
15 
16 class imgIRequest;
17 namespace mozilla {
18 struct MediaFeatureChange;
19 }
20 
21 namespace mozilla {
22 namespace dom {
23 
24 /*
25  * Image Tracking
26  *
27  * Style and content images register their imgIRequests with their document's
28  * image tracker, so that we can efficiently tell all descendant images when
29  * they are and are not visible. When an image is on-screen, we want to call
30  * LockImage() on it so that it doesn't do things like discarding frame data
31  * to save memory. The PresShell informs its document's image tracker whether
32  * its images should be locked or not via SetLockingState().
33  *
34  * See bug 512260.
35  */
36 class ImageTracker {
37  public:
38   ImageTracker();
39   ImageTracker(const ImageTracker&) = delete;
40   ImageTracker& operator=(const ImageTracker&) = delete;
41 
42   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ImageTracker)
43 
44   nsresult Add(imgIRequest* aImage);
45 
46   enum { REQUEST_DISCARD = 0x1 };
47   nsresult Remove(imgIRequest* aImage, uint32_t aFlags = 0);
48 
49   // Makes the images on this document locked/unlocked. By default, the locking
50   // state is unlocked/false.
51   nsresult SetLockingState(bool aLocked);
52 
53   // Makes the images on this document capable of having their animation
54   // active or suspended. An Image will animate as long as at least one of its
55   // owning Documents needs it to animate; otherwise it can suspend.
56   void SetAnimatingState(bool aAnimating);
57 
58   void RequestDiscardAll();
59   void MediaFeatureValuesChangedAllDocuments(const MediaFeatureChange&);
60 
61  private:
62   ~ImageTracker();
63 
64   nsDataHashtable<nsPtrHashKey<imgIRequest>, uint32_t> mImages;
65   bool mLocking;
66   bool mAnimating;
67 };
68 
69 }  // namespace dom
70 }  // namespace mozilla
71 
72 #endif  // mozilla_dom_ImageTracker
73