Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / cc / tiles / decoded_image_tracker.cc
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.
4
5 #include "cc/tiles/decoded_image_tracker.h"
6 #include "base/trace_event/trace_event.h"
7
8 namespace cc {
9 namespace {
10 const int kNumFramesToLock = 2;
11 }  // namespace
12
13 DecodedImageTracker::DecodedImageTracker() = default;
14 DecodedImageTracker::~DecodedImageTracker() {
15   for (auto& pair : locked_images_)
16     image_controller_->UnlockImageDecode(pair.first);
17 }
18
19 void DecodedImageTracker::QueueImageDecode(
20     const PaintImage& image,
21     const gfx::ColorSpace& target_color_space,
22     const base::Callback<void(bool)>& callback) {
23   TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
24                "DecodedImageTracker::QueueImageDecode", "frame_key",
25                image.GetKeyForFrame(image.frame_index()).ToString());
26   DCHECK(image_controller_);
27   // Queue the decode in the image controller, but switch out the callback for
28   // our own.
29   auto image_bounds = SkIRect::MakeWH(image.width(), image.height());
30   DrawImage draw_image(image, image_bounds, kNone_SkFilterQuality,
31                        SkMatrix::I(), image.frame_index(), target_color_space);
32   image_controller_->QueueImageDecode(
33       draw_image, base::Bind(&DecodedImageTracker::ImageDecodeFinished,
34                              base::Unretained(this), callback));
35 }
36
37 void DecodedImageTracker::NotifyFrameFinished() {
38   // Go through the images and if the frame ref count goes to 0, unlock the
39   // image in the controller.
40   for (auto it = locked_images_.begin(); it != locked_images_.end();) {
41     auto id = it->first;
42     int& ref_count = it->second;
43     if (--ref_count != 0) {
44       ++it;
45       continue;
46     }
47     image_controller_->UnlockImageDecode(id);
48     it = locked_images_.erase(it);
49   }
50 }
51
52 void DecodedImageTracker::ImageDecodeFinished(
53     const base::Callback<void(bool)>& callback,
54     ImageController::ImageDecodeRequestId id,
55     ImageController::ImageDecodeResult result) {
56   TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
57                "DecodedImageTracker::ImageDecodeFinished");
58
59   if (result == ImageController::ImageDecodeResult::SUCCESS)
60     locked_images_.push_back(std::make_pair(id, kNumFramesToLock));
61   bool decode_succeeded =
62       result == ImageController::ImageDecodeResult::SUCCESS ||
63       result == ImageController::ImageDecodeResult::DECODE_NOT_REQUIRED;
64   callback.Run(decode_succeeded);
65 }
66
67 }  // namespace cc