Popular sites on the NTP: Favicon improvements
[chromium-blink-merge.git] / cc / layers / delegated_frame_resource_collection.cc
bloba1bc759a0330eb531601eb19ddab2a0d09c72fd7
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.
5 #include "cc/layers/delegated_frame_resource_collection.h"
7 #include "base/bind.h"
8 #include "cc/trees/blocking_task_runner.h"
10 namespace cc {
12 DelegatedFrameResourceCollection::DelegatedFrameResourceCollection()
13 : client_(nullptr), lost_all_resources_(false), weak_ptr_factory_(this) {
14 DCHECK(main_thread_checker_.CalledOnValidThread());
17 DelegatedFrameResourceCollection::~DelegatedFrameResourceCollection() {
18 DCHECK(main_thread_checker_.CalledOnValidThread());
21 void DelegatedFrameResourceCollection::SetClient(
22 DelegatedFrameResourceCollectionClient* client) {
23 client_ = client;
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_);
33 bool DelegatedFrameResourceCollection::LoseAllResources() {
34 DCHECK(main_thread_checker_.CalledOnValidThread());
35 DCHECK(!lost_all_resources_);
36 lost_all_resources_ = true;
38 if (resource_id_ref_count_map_.empty())
39 return false;
41 ReturnedResourceArray to_return;
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);
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);
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;
64 void DelegatedFrameResourceCollection::ReceivedResources(
65 const TransferableResourceArray& resources) {
66 DCHECK(main_thread_checker_.CalledOnValidThread());
67 DCHECK(!lost_all_resources_);
69 for (size_t i = 0; i < resources.size(); ++i)
70 resource_id_ref_count_map_[resources[i].id].refs_to_return++;
73 void DelegatedFrameResourceCollection::UnrefResources(
74 const ReturnedResourceArray& returned) {
75 DCHECK(main_thread_checker_.CalledOnValidThread());
77 if (lost_all_resources_)
78 return;
80 ReturnedResourceArray to_return;
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);
95 if (to_return.empty())
96 return;
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();
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++;
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));
123 ReturnCallback
124 DelegatedFrameResourceCollection::GetReturnResourcesCallbackForImplThread() {
125 return base::Bind(&UnrefResourcesOnImplThread,
126 weak_ptr_factory_.GetWeakPtr());
129 } // namespace cc