1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/tiles/decoded_image_tracker.h"
6 #include "base/time/default_tick_clock.h"
7 #include "base/trace_event/trace_event.h"
11 // Timeout images after 250ms, whether or not they've been used. This prevents
12 // unbounded cache usage.
13 const int64_t kTimeoutDurationMs = 250;
16 DecodedImageTracker::ImageLock::ImageLock(
17 DecodedImageTracker* tracker,
18 ImageController::ImageDecodeRequestId request_id,
19 base::TimeTicks lock_time)
20 : tracker_(tracker), request_id_(request_id), lock_time_(lock_time) {}
22 DecodedImageTracker::ImageLock::~ImageLock() {
23 tracker_->image_controller_->UnlockImageDecode(request_id_);
26 DecodedImageTracker::DecodedImageTracker(
27 ImageController* controller,
28 scoped_refptr<base::SequencedTaskRunner> task_runner)
29 : image_controller_(controller),
30 task_runner_(std::move(task_runner)),
31 tick_clock_(base::DefaultTickClock::GetInstance()) {
32 DCHECK(image_controller_);
35 DecodedImageTracker::~DecodedImageTracker() {
39 void DecodedImageTracker::QueueImageDecode(
40 const PaintImage& image,
41 const gfx::ColorSpace& target_color_space,
42 base::OnceCallback<void(bool)> callback) {
43 size_t frame_index = PaintImage::kDefaultFrameIndex;
44 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
45 "DecodedImageTracker::QueueImageDecode", "frame_key",
46 image.GetKeyForFrame(frame_index).ToString());
47 DCHECK(image_controller_);
48 // Queue the decode in the image controller, but switch out the callback for
50 auto image_bounds = SkIRect::MakeWH(image.width(), image.height());
51 DrawImage draw_image(image, false, image_bounds,
52 PaintFlags::FilterQuality::kNone, SkM44(), frame_index,
54 image_controller_->QueueImageDecode(
55 draw_image, base::BindOnce(&DecodedImageTracker::ImageDecodeFinished,
56 base::Unretained(this), std::move(callback),
60 void DecodedImageTracker::UnlockAllImages() {
61 locked_images_.clear();
64 void DecodedImageTracker::OnImagesUsedInDraw(
65 const std::vector<DrawImage>& draw_images) {
66 for (const DrawImage& draw_image : draw_images)
67 locked_images_.erase(draw_image.paint_image().stable_id());
70 void DecodedImageTracker::ImageDecodeFinished(
71 base::OnceCallback<void(bool)> callback,
72 PaintImage::Id image_id,
73 ImageController::ImageDecodeRequestId request_id,
74 ImageController::ImageDecodeResult result) {
75 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
76 "DecodedImageTracker::ImageDecodeFinished");
78 if (result == ImageController::ImageDecodeResult::SUCCESS) {
79 // If this image already exists, just replace it with the new (latest)
81 locked_images_.erase(image_id);
82 locked_images_.emplace(
84 std::make_unique<ImageLock>(this, request_id, tick_clock_->NowTicks()));
87 bool decode_succeeded =
88 result == ImageController::ImageDecodeResult::SUCCESS ||
89 result == ImageController::ImageDecodeResult::DECODE_NOT_REQUIRED;
90 std::move(callback).Run(decode_succeeded);
93 void DecodedImageTracker::OnTimeoutImages() {
94 timeout_pending_ = false;
95 if (locked_images_.size() == 0)
98 auto now = tick_clock_->NowTicks();
99 auto timeout = base::TimeDelta::FromMilliseconds(kTimeoutDurationMs);
100 for (auto it = locked_images_.begin(); it != locked_images_.end();) {
101 auto& image = it->second;
102 if (now - image->lock_time() < timeout) {
106 it = locked_images_.erase(it);
112 void DecodedImageTracker::EnqueueTimeout() {
113 if (timeout_pending_)
115 if (locked_images_.size() == 0)
118 timeout_pending_ = true;
119 task_runner_->PostDelayedTask(
121 base::BindOnce(&DecodedImageTracker::OnTimeoutImages,
122 weak_ptr_factory_.GetWeakPtr()),
123 base::TimeDelta::FromMilliseconds(kTimeoutDurationMs));