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, image_bounds, kNone_SkFilterQuality,
52 SkMatrix::I(), frame_index, target_color_space);
53 image_controller_->QueueImageDecode(
54 draw_image, base::BindOnce(&DecodedImageTracker::ImageDecodeFinished,
55 base::Unretained(this), std::move(callback),
59 void DecodedImageTracker::UnlockAllImages() {
60 locked_images_.clear();
63 void DecodedImageTracker::OnImagesUsedInDraw(
64 const std::vector<DrawImage>& draw_images) {
65 for (const DrawImage& draw_image : draw_images)
66 locked_images_.erase(draw_image.paint_image().stable_id());
69 void DecodedImageTracker::ImageDecodeFinished(
70 base::OnceCallback<void(bool)> callback,
71 PaintImage::Id image_id,
72 ImageController::ImageDecodeRequestId request_id,
73 ImageController::ImageDecodeResult result) {
74 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
75 "DecodedImageTracker::ImageDecodeFinished");
77 if (result == ImageController::ImageDecodeResult::SUCCESS) {
78 // If this image already exists, just replace it with the new (latest)
80 locked_images_.erase(image_id);
81 locked_images_.emplace(
83 std::make_unique<ImageLock>(this, request_id, tick_clock_->NowTicks()));
86 bool decode_succeeded =
87 result == ImageController::ImageDecodeResult::SUCCESS ||
88 result == ImageController::ImageDecodeResult::DECODE_NOT_REQUIRED;
89 std::move(callback).Run(decode_succeeded);
92 void DecodedImageTracker::OnTimeoutImages() {
93 timeout_pending_ = false;
94 if (locked_images_.size() == 0)
97 auto now = tick_clock_->NowTicks();
98 auto timeout = base::TimeDelta::FromMilliseconds(kTimeoutDurationMs);
99 for (auto it = locked_images_.begin(); it != locked_images_.end();) {
100 auto& image = it->second;
101 if (now - image->lock_time() < timeout) {
105 it = locked_images_.erase(it);
111 void DecodedImageTracker::EnqueueTimeout() {
112 if (timeout_pending_)
114 if (locked_images_.size() == 0)
117 timeout_pending_ = true;
118 task_runner_->PostDelayedTask(
120 base::BindOnce(&DecodedImageTracker::OnTimeoutImages,
121 weak_ptr_factory_.GetWeakPtr()),
122 base::TimeDelta::FromMilliseconds(kTimeoutDurationMs));