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"
8 #include "cc/trees/blocking_task_runner.h"
12 DelegatedFrameResourceCollection::DelegatedFrameResourceCollection()
14 main_thread_runner_(BlockingTaskRunner::current()),
15 lost_all_resources_(false),
16 weak_ptr_factory_(this) {
17 DCHECK(main_thread_checker_.CalledOnValidThread());
20 DelegatedFrameResourceCollection::~DelegatedFrameResourceCollection() {
21 DCHECK(main_thread_checker_.CalledOnValidThread());
24 void DelegatedFrameResourceCollection::SetClient(
25 DelegatedFrameResourceCollectionClient* client) {
29 void DelegatedFrameResourceCollection::TakeUnusedResourcesForChildCompositor(
30 ReturnedResourceArray* array) {
31 DCHECK(main_thread_checker_.CalledOnValidThread());
32 DCHECK(array->empty());
33 array->swap(returned_resources_for_child_compositor_);
36 bool DelegatedFrameResourceCollection::LoseAllResources() {
37 DCHECK(main_thread_checker_.CalledOnValidThread());
38 DCHECK(!lost_all_resources_);
39 lost_all_resources_ = true;
41 if (resource_id_ref_count_map_.empty())
44 ReturnedResourceArray to_return;
46 for (ResourceIdRefCountMap::iterator it = resource_id_ref_count_map_.begin();
47 it != resource_id_ref_count_map_.end();
49 DCHECK_GE(it->second.refs_to_wait_for, 1);
51 ReturnedResource returned;
52 returned.id = it->first;
53 returned.count = it->second.refs_to_return;
55 to_return.push_back(returned);
58 returned_resources_for_child_compositor_.insert(
59 returned_resources_for_child_compositor_.end(),
63 client_->UnusedResourcesAreAvailable();
67 void DelegatedFrameResourceCollection::ReceivedResources(
68 const TransferableResourceArray& resources) {
69 DCHECK(main_thread_checker_.CalledOnValidThread());
70 DCHECK(!lost_all_resources_);
72 for (size_t i = 0; i < resources.size(); ++i)
73 resource_id_ref_count_map_[resources[i].id].refs_to_return++;
76 void DelegatedFrameResourceCollection::UnrefResources(
77 const ReturnedResourceArray& returned) {
78 DCHECK(main_thread_checker_.CalledOnValidThread());
80 if (lost_all_resources_)
83 ReturnedResourceArray to_return;
85 for (size_t i = 0; i < returned.size(); ++i) {
86 ResourceIdRefCountMap::iterator it =
87 resource_id_ref_count_map_.find(returned[i].id);
88 DCHECK(it != resource_id_ref_count_map_.end());
89 DCHECK_GE(it->second.refs_to_wait_for, returned[i].count);
90 it->second.refs_to_wait_for -= returned[i].count;
91 if (it->second.refs_to_wait_for == 0) {
92 to_return.push_back(returned[i]);
93 to_return.back().count = it->second.refs_to_return;
94 resource_id_ref_count_map_.erase(it);
98 if (to_return.empty())
101 returned_resources_for_child_compositor_.insert(
102 returned_resources_for_child_compositor_.end(),
106 client_->UnusedResourcesAreAvailable();
109 void DelegatedFrameResourceCollection::RefResources(
110 const TransferableResourceArray& resources) {
111 DCHECK(main_thread_checker_.CalledOnValidThread());
112 for (size_t i = 0; i < resources.size(); ++i)
113 resource_id_ref_count_map_[resources[i].id].refs_to_wait_for++;
116 static void UnrefResourcesOnImplThread(
117 base::WeakPtr<DelegatedFrameResourceCollection> self,
118 scoped_refptr<BlockingTaskRunner> main_thread_runner,
119 const ReturnedResourceArray& returned) {
120 main_thread_runner->PostTask(
123 &DelegatedFrameResourceCollection::UnrefResources, self, returned));
127 DelegatedFrameResourceCollection::GetReturnResourcesCallbackForImplThread() {
128 return base::Bind(&UnrefResourcesOnImplThread,
129 weak_ptr_factory_.GetWeakPtr(),
130 main_thread_runner_);