Update V8 to version 4.7.44.
[chromium-blink-merge.git] / cc / tiles / image_decode_controller.cc
blob601f3a0bb77b5fed99da519e30cb7dc1b7760909
1 // Copyright 2015 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/image_decode_controller.h"
7 #include "cc/debug/devtools_instrumentation.h"
9 namespace cc {
10 namespace {
12 class ImageDecodeTaskImpl : public ImageDecodeTask {
13 public:
14 ImageDecodeTaskImpl(ImageDecodeController* controller,
15 const SkImage* image,
16 int layer_id,
17 uint64_t source_prepare_tiles_id)
18 : controller_(controller),
19 image_(skia::SharePtr(image)),
20 layer_id_(layer_id),
21 source_prepare_tiles_id_(source_prepare_tiles_id) {}
23 // Overridden from Task:
24 void RunOnWorkerThread() override {
25 TRACE_EVENT1("cc", "ImageDecodeTaskImpl::RunOnWorkerThread",
26 "source_prepare_tiles_id", source_prepare_tiles_id_);
27 devtools_instrumentation::ScopedImageDecodeTask image_decode_task(
28 image_.get());
29 controller_->DecodeImage(image_.get());
31 // Release the reference after decoding image to ensure that it is not kept
32 // alive unless needed.
33 image_.clear();
36 // Overridden from TileTask:
37 void ScheduleOnOriginThread(TileTaskClient* client) override {}
38 void CompleteOnOriginThread(TileTaskClient* client) override {
39 controller_->OnImageDecodeTaskCompleted(layer_id_, image_.get(),
40 !HasFinishedRunning());
43 protected:
44 ~ImageDecodeTaskImpl() override {}
46 private:
47 ImageDecodeController* controller_;
48 skia::RefPtr<const SkImage> image_;
49 int layer_id_;
50 uint64_t source_prepare_tiles_id_;
52 DISALLOW_COPY_AND_ASSIGN(ImageDecodeTaskImpl);
55 } // namespace
57 ImageDecodeController::ImageDecodeController() {}
59 ImageDecodeController::~ImageDecodeController() {}
61 scoped_refptr<ImageDecodeTask> ImageDecodeController::GetTaskForImage(
62 const skia::PositionImage& image,
63 int layer_id,
64 uint64_t prepare_tiles_id) {
65 uint32_t generation_id = image.image->uniqueID();
66 scoped_refptr<ImageDecodeTask>& decode_task =
67 image_decode_tasks_[layer_id][generation_id];
68 if (!decode_task)
69 decode_task = CreateTaskForImage(image.image, layer_id, prepare_tiles_id);
70 return decode_task;
73 scoped_refptr<ImageDecodeTask> ImageDecodeController::CreateTaskForImage(
74 const SkImage* image,
75 int layer_id,
76 uint64_t prepare_tiles_id) {
77 return make_scoped_refptr(
78 new ImageDecodeTaskImpl(this, image, layer_id, prepare_tiles_id));
81 void ImageDecodeController::DecodeImage(const SkImage* image) {
82 image->preroll();
85 void ImageDecodeController::AddLayerUsedCount(int layer_id) {
86 ++used_layer_counts_[layer_id];
89 void ImageDecodeController::SubtractLayerUsedCount(int layer_id) {
90 if (--used_layer_counts_[layer_id])
91 return;
93 // Clean up decode tasks once a layer is no longer used.
94 used_layer_counts_.erase(layer_id);
95 image_decode_tasks_.erase(layer_id);
98 void ImageDecodeController::OnImageDecodeTaskCompleted(int layer_id,
99 const SkImage* image,
100 bool was_canceled) {
101 // If the task has successfully finished, then keep the task until the layer
102 // is no longer in use. This ensures that we only decode a image once.
103 // TODO(vmpstr): Remove this when decode lifetime is controlled by cc.
104 if (!was_canceled)
105 return;
107 // Otherwise, we have to clean up the task so that a new one can be created if
108 // we need to decode the image again.
109 LayerImageTaskMap::iterator layer_it = image_decode_tasks_.find(layer_id);
110 if (layer_it == image_decode_tasks_.end())
111 return;
113 ImageTaskMap& image_tasks = layer_it->second;
114 ImageTaskMap::iterator task_it = image_tasks.find(image->uniqueID());
115 if (task_it == image_tasks.end())
116 return;
117 image_tasks.erase(task_it);
120 } // namespace cc