[M108 Migration][VD] Avoid pending frame counter becoming negative
[platform/framework/web/chromium-efl.git] / cc / tiles / decoded_image_tracker.h
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CC_TILES_DECODED_IMAGE_TRACKER_H_
6 #define CC_TILES_DECODED_IMAGE_TRACKER_H_
7
8 #include <memory>
9 #include <utility>
10 #include <vector>
11
12 #include "base/bind.h"
13 #include "base/memory/raw_ptr.h"
14 #include "base/time/tick_clock.h"
15 #include "base/time/time.h"
16 #include "cc/cc_export.h"
17 #include "cc/paint/target_color_params.h"
18 #include "cc/tiles/image_controller.h"
19
20 namespace cc {
21
22 // This class is the main interface for the rest of the system to request
23 // decodes. It is responsible for keeping the decodes locked for a number of
24 // frames, specified as |kNumFramesToLock| in the implementation file.
25 //
26 // Note that it is safe to replace ImageController's cache without doing
27 // anything special with this class, since it retains only ids to the decode
28 // requests. When defunct ids are then used to try and unlock the image, they
29 // are silently ignored.
30 class CC_EXPORT DecodedImageTracker {
31  public:
32   explicit DecodedImageTracker(
33       ImageController* controller,
34       scoped_refptr<base::SequencedTaskRunner> task_runner);
35   DecodedImageTracker(const DecodedImageTracker&) = delete;
36   ~DecodedImageTracker();
37
38   DecodedImageTracker& operator=(const DecodedImageTracker&) = delete;
39
40   // Request that the given image be decoded. This issues a callback upon
41   // completion. The callback takes a bool indicating whether the decode was
42   // successful or not.
43   void QueueImageDecode(const PaintImage& image,
44                         const TargetColorParams& target_color_params,
45                         base::OnceCallback<void(bool)> callback);
46
47   // Unlock all locked images - used to respond to memory pressure or
48   // application background.
49   void UnlockAllImages();
50
51   // Notifies the tracker that images have been used, allowing it to
52   // unlock them.
53   void OnImagesUsedInDraw(const std::vector<DrawImage>& draw_images);
54
55   void SetTickClockForTesting(const base::TickClock* tick_clock) {
56     tick_clock_ = tick_clock;
57   }
58
59   // Test only functions:
60   size_t NumLockedImagesForTesting() const { return locked_images_.size(); }
61
62  private:
63   friend class DecodedImageTrackerTest;
64
65   void ImageDecodeFinished(base::OnceCallback<void(bool)> callback,
66                            PaintImage::Id image_id,
67                            ImageController::ImageDecodeRequestId request_id,
68                            ImageController::ImageDecodeResult result);
69   void OnTimeoutImages();
70   void EnqueueTimeout();
71
72   raw_ptr<ImageController> image_controller_;
73
74   // Helper class tracking a locked image decode. Automatically releases the
75   // lock using the provided DecodedImageTracker* on destruction.
76   class ImageLock {
77    public:
78     ImageLock(DecodedImageTracker* tracker,
79               ImageController::ImageDecodeRequestId request_id,
80               base::TimeTicks lock_time);
81     ImageLock(const ImageLock&) = delete;
82     ~ImageLock();
83
84     ImageLock& operator=(const ImageLock&) = delete;
85     base::TimeTicks lock_time() const { return lock_time_; }
86
87    private:
88     raw_ptr<DecodedImageTracker> tracker_;
89     ImageController::ImageDecodeRequestId request_id_;
90     base::TimeTicks lock_time_;
91   };
92   base::flat_map<PaintImage::Id, std::unique_ptr<ImageLock>> locked_images_;
93   bool timeout_pending_ = false;
94   scoped_refptr<base::SequencedTaskRunner> task_runner_;
95
96   // Defaults to base::TimeTicks::Now(), but overrideable for testing.
97   raw_ptr<const base::TickClock> tick_clock_;
98
99   base::WeakPtrFactory<DecodedImageTracker> weak_ptr_factory_{this};
100 };
101
102 }  // namespace cc
103
104 #endif  // CC_TILES_DECODED_IMAGE_TRACKER_H_