[sql] Record SQLite memory usage after a week.
[chromium-blink-merge.git] / cc / tiles / image_decode_controller.cc
blob9b3d1723b4f4effc542ebd8c4bd870f48defd7e5
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 SkPixelRef* pixel_ref,
16 int layer_id,
17 uint64_t source_prepare_tiles_id)
18 : controller_(controller),
19 pixel_ref_(skia::SharePtr(pixel_ref)),
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 pixel_ref_.get());
29 controller_->DecodePixelRef(pixel_ref_.get());
31 // Release the reference after decoding image to ensure that it is not kept
32 // alive unless needed.
33 pixel_ref_.clear();
36 // Overridden from TileTask:
37 void ScheduleOnOriginThread(TileTaskClient* client) override {}
38 void CompleteOnOriginThread(TileTaskClient* client) override {}
39 void RunReplyOnOriginThread() override {
40 controller_->OnImageDecodeTaskCompleted(layer_id_, pixel_ref_.get(),
41 !HasFinishedRunning());
44 protected:
45 ~ImageDecodeTaskImpl() override {}
47 private:
48 ImageDecodeController* controller_;
49 skia::RefPtr<SkPixelRef> pixel_ref_;
50 int layer_id_;
51 uint64_t source_prepare_tiles_id_;
53 DISALLOW_COPY_AND_ASSIGN(ImageDecodeTaskImpl);
56 } // namespace
58 ImageDecodeController::ImageDecodeController() {}
60 ImageDecodeController::~ImageDecodeController() {}
62 scoped_refptr<ImageDecodeTask> ImageDecodeController::GetTaskForPixelRef(
63 const skia::PositionPixelRef& pixel_ref,
64 int layer_id,
65 uint64_t prepare_tiles_id) {
66 uint32_t generation_id = pixel_ref.pixel_ref->getGenerationID();
67 scoped_refptr<ImageDecodeTask>& decode_task =
68 image_decode_tasks_[layer_id][generation_id];
69 if (!decode_task)
70 decode_task =
71 CreateTaskForPixelRef(pixel_ref.pixel_ref, layer_id, prepare_tiles_id);
72 return decode_task;
75 scoped_refptr<ImageDecodeTask> ImageDecodeController::CreateTaskForPixelRef(
76 SkPixelRef* pixel_ref,
77 int layer_id,
78 uint64_t prepare_tiles_id) {
79 return make_scoped_refptr(
80 new ImageDecodeTaskImpl(this, pixel_ref, layer_id, prepare_tiles_id));
83 void ImageDecodeController::DecodePixelRef(SkPixelRef* pixel_ref) {
84 // This will cause the image referred to by pixel ref to be decoded.
85 pixel_ref->lockPixels();
86 pixel_ref->unlockPixels();
89 void ImageDecodeController::AddLayerUsedCount(int layer_id) {
90 ++used_layer_counts_[layer_id];
93 void ImageDecodeController::SubtractLayerUsedCount(int layer_id) {
94 if (--used_layer_counts_[layer_id])
95 return;
97 // Clean up decode tasks once a layer is no longer used.
98 used_layer_counts_.erase(layer_id);
99 image_decode_tasks_.erase(layer_id);
102 void ImageDecodeController::OnImageDecodeTaskCompleted(int layer_id,
103 SkPixelRef* pixel_ref,
104 bool was_canceled) {
105 // If the task has successfully finished, then keep the task until the layer
106 // is no longer in use. This ensures that we only decode a pixel ref once.
107 // TODO(vmpstr): Remove this when decode lifetime is controlled by cc.
108 if (!was_canceled)
109 return;
111 // Otherwise, we have to clean up the task so that a new one can be created if
112 // we need to decode the pixel ref again.
113 LayerPixelRefTaskMap::iterator layer_it = image_decode_tasks_.find(layer_id);
114 if (layer_it == image_decode_tasks_.end())
115 return;
117 PixelRefTaskMap& pixel_ref_tasks = layer_it->second;
118 PixelRefTaskMap::iterator task_it =
119 pixel_ref_tasks.find(pixel_ref->getGenerationID());
120 if (task_it == pixel_ref_tasks.end())
121 return;
122 pixel_ref_tasks.erase(task_it);
125 } // namespace cc