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.
5 #ifndef CC_TILES_DECODED_IMAGE_TRACKER_H_
6 #define CC_TILES_DECODED_IMAGE_TRACKER_H_
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"
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.
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 {
32 explicit DecodedImageTracker(
33 ImageController* controller,
34 scoped_refptr<base::SequencedTaskRunner> task_runner);
35 DecodedImageTracker(const DecodedImageTracker&) = delete;
36 ~DecodedImageTracker();
38 DecodedImageTracker& operator=(const DecodedImageTracker&) = delete;
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
43 void QueueImageDecode(const PaintImage& image,
44 const TargetColorParams& target_color_params,
45 base::OnceCallback<void(bool)> callback);
47 // Unlock all locked images - used to respond to memory pressure or
48 // application background.
49 void UnlockAllImages();
51 // Notifies the tracker that images have been used, allowing it to
53 void OnImagesUsedInDraw(const std::vector<DrawImage>& draw_images);
55 void SetTickClockForTesting(const base::TickClock* tick_clock) {
56 tick_clock_ = tick_clock;
59 // Test only functions:
60 size_t NumLockedImagesForTesting() const { return locked_images_.size(); }
63 friend class DecodedImageTrackerTest;
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();
72 raw_ptr<ImageController> image_controller_;
74 // Helper class tracking a locked image decode. Automatically releases the
75 // lock using the provided DecodedImageTracker* on destruction.
78 ImageLock(DecodedImageTracker* tracker,
79 ImageController::ImageDecodeRequestId request_id,
80 base::TimeTicks lock_time);
81 ImageLock(const ImageLock&) = delete;
84 ImageLock& operator=(const ImageLock&) = delete;
85 base::TimeTicks lock_time() const { return lock_time_; }
88 raw_ptr<DecodedImageTracker> tracker_;
89 ImageController::ImageDecodeRequestId request_id_;
90 base::TimeTicks lock_time_;
92 base::flat_map<PaintImage::Id, std::unique_ptr<ImageLock>> locked_images_;
93 bool timeout_pending_ = false;
94 scoped_refptr<base::SequencedTaskRunner> task_runner_;
96 // Defaults to base::TimeTicks::Now(), but overrideable for testing.
97 raw_ptr<const base::TickClock> tick_clock_;
99 base::WeakPtrFactory<DecodedImageTracker> weak_ptr_factory_{this};
104 #endif // CC_TILES_DECODED_IMAGE_TRACKER_H_