a1bc759a0330eb531601eb19ddab2a0d09c72fd7
[platform/framework/web/crosswalk.git] / src / cc / layers / delegated_frame_resource_collection.cc
1 // Copyright 2013 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/layers/delegated_frame_resource_collection.h"
6
7 #include "base/bind.h"
8 #include "cc/trees/blocking_task_runner.h"
9
10 namespace cc {
11
12 DelegatedFrameResourceCollection::DelegatedFrameResourceCollection()
13     : client_(nullptr), lost_all_resources_(false), weak_ptr_factory_(this) {
14   DCHECK(main_thread_checker_.CalledOnValidThread());
15 }
16
17 DelegatedFrameResourceCollection::~DelegatedFrameResourceCollection() {
18   DCHECK(main_thread_checker_.CalledOnValidThread());
19 }
20
21 void DelegatedFrameResourceCollection::SetClient(
22     DelegatedFrameResourceCollectionClient* client) {
23   client_ = client;
24 }
25
26 void DelegatedFrameResourceCollection::TakeUnusedResourcesForChildCompositor(
27     ReturnedResourceArray* array) {
28   DCHECK(main_thread_checker_.CalledOnValidThread());
29   DCHECK(array->empty());
30   array->swap(returned_resources_for_child_compositor_);
31 }
32
33 bool DelegatedFrameResourceCollection::LoseAllResources() {
34   DCHECK(main_thread_checker_.CalledOnValidThread());
35   DCHECK(!lost_all_resources_);
36   lost_all_resources_ = true;
37
38   if (resource_id_ref_count_map_.empty())
39     return false;
40
41   ReturnedResourceArray to_return;
42
43   for (ResourceIdRefCountMap::iterator it = resource_id_ref_count_map_.begin();
44        it != resource_id_ref_count_map_.end();
45        ++it) {
46     DCHECK_GE(it->second.refs_to_wait_for, 1);
47
48     ReturnedResource returned;
49     returned.id = it->first;
50     returned.count = it->second.refs_to_return;
51     returned.lost = true;
52     to_return.push_back(returned);
53   }
54
55   returned_resources_for_child_compositor_.insert(
56       returned_resources_for_child_compositor_.end(),
57       to_return.begin(),
58       to_return.end());
59   if (client_)
60     client_->UnusedResourcesAreAvailable();
61   return true;
62 }
63
64 void DelegatedFrameResourceCollection::ReceivedResources(
65     const TransferableResourceArray& resources) {
66   DCHECK(main_thread_checker_.CalledOnValidThread());
67   DCHECK(!lost_all_resources_);
68
69   for (size_t i = 0; i < resources.size(); ++i)
70     resource_id_ref_count_map_[resources[i].id].refs_to_return++;
71 }
72
73 void DelegatedFrameResourceCollection::UnrefResources(
74     const ReturnedResourceArray& returned) {
75   DCHECK(main_thread_checker_.CalledOnValidThread());
76
77   if (lost_all_resources_)
78     return;
79
80   ReturnedResourceArray to_return;
81
82   for (size_t i = 0; i < returned.size(); ++i) {
83     ResourceIdRefCountMap::iterator it =
84         resource_id_ref_count_map_.find(returned[i].id);
85     DCHECK(it != resource_id_ref_count_map_.end());
86     DCHECK_GE(it->second.refs_to_wait_for, returned[i].count);
87     it->second.refs_to_wait_for -= returned[i].count;
88     if (it->second.refs_to_wait_for == 0) {
89       to_return.push_back(returned[i]);
90       to_return.back().count = it->second.refs_to_return;
91       resource_id_ref_count_map_.erase(it);
92     }
93   }
94
95   if (to_return.empty())
96     return;
97
98   returned_resources_for_child_compositor_.insert(
99       returned_resources_for_child_compositor_.end(),
100       to_return.begin(),
101       to_return.end());
102   if (client_)
103     client_->UnusedResourcesAreAvailable();
104 }
105
106 void DelegatedFrameResourceCollection::RefResources(
107     const TransferableResourceArray& resources) {
108   DCHECK(main_thread_checker_.CalledOnValidThread());
109   for (size_t i = 0; i < resources.size(); ++i)
110     resource_id_ref_count_map_[resources[i].id].refs_to_wait_for++;
111 }
112
113 static void UnrefResourcesOnImplThread(
114     base::WeakPtr<DelegatedFrameResourceCollection> self,
115     const ReturnedResourceArray& returned,
116     BlockingTaskRunner* main_thread_task_runner) {
117   main_thread_task_runner->PostTask(
118       FROM_HERE,
119       base::Bind(
120           &DelegatedFrameResourceCollection::UnrefResources, self, returned));
121 }
122
123 ReturnCallback
124 DelegatedFrameResourceCollection::GetReturnResourcesCallbackForImplThread() {
125   return base::Bind(&UnrefResourcesOnImplThread,
126                     weak_ptr_factory_.GetWeakPtr());
127 }
128
129 }  // namespace cc