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 lost_all_resources_(false),
15 weak_ptr_factory_(this) {
16 DCHECK(main_thread_checker_
.CalledOnValidThread());
19 DelegatedFrameResourceCollection::~DelegatedFrameResourceCollection() {
20 DCHECK(main_thread_checker_
.CalledOnValidThread());
23 void DelegatedFrameResourceCollection::SetClient(
24 DelegatedFrameResourceCollectionClient
* client
) {
28 void DelegatedFrameResourceCollection::TakeUnusedResourcesForChildCompositor(
29 ReturnedResourceArray
* array
) {
30 DCHECK(main_thread_checker_
.CalledOnValidThread());
31 DCHECK(array
->empty());
32 array
->swap(returned_resources_for_child_compositor_
);
35 bool DelegatedFrameResourceCollection::LoseAllResources() {
36 DCHECK(main_thread_checker_
.CalledOnValidThread());
37 DCHECK(!lost_all_resources_
);
38 lost_all_resources_
= true;
40 if (resource_id_ref_count_map_
.empty())
43 ReturnedResourceArray to_return
;
45 for (ResourceIdRefCountMap::iterator it
= resource_id_ref_count_map_
.begin();
46 it
!= resource_id_ref_count_map_
.end();
48 DCHECK_GE(it
->second
.refs_to_wait_for
, 1);
50 ReturnedResource returned
;
51 returned
.id
= it
->first
;
52 returned
.count
= it
->second
.refs_to_return
;
54 to_return
.push_back(returned
);
57 returned_resources_for_child_compositor_
.insert(
58 returned_resources_for_child_compositor_
.end(),
62 client_
->UnusedResourcesAreAvailable();
66 void DelegatedFrameResourceCollection::ReceivedResources(
67 const TransferableResourceArray
& resources
) {
68 DCHECK(main_thread_checker_
.CalledOnValidThread());
69 DCHECK(!lost_all_resources_
);
71 for (size_t i
= 0; i
< resources
.size(); ++i
)
72 resource_id_ref_count_map_
[resources
[i
].id
].refs_to_return
++;
75 void DelegatedFrameResourceCollection::UnrefResources(
76 const ReturnedResourceArray
& returned
) {
77 DCHECK(main_thread_checker_
.CalledOnValidThread());
79 if (lost_all_resources_
)
82 ReturnedResourceArray to_return
;
84 for (size_t i
= 0; i
< returned
.size(); ++i
) {
85 ResourceIdRefCountMap::iterator it
=
86 resource_id_ref_count_map_
.find(returned
[i
].id
);
87 DCHECK(it
!= resource_id_ref_count_map_
.end());
88 DCHECK_GE(it
->second
.refs_to_wait_for
, returned
[i
].count
);
89 it
->second
.refs_to_wait_for
-= returned
[i
].count
;
90 if (it
->second
.refs_to_wait_for
== 0) {
91 to_return
.push_back(returned
[i
]);
92 to_return
.back().count
= it
->second
.refs_to_return
;
93 resource_id_ref_count_map_
.erase(it
);
97 if (to_return
.empty())
100 returned_resources_for_child_compositor_
.insert(
101 returned_resources_for_child_compositor_
.end(),
105 client_
->UnusedResourcesAreAvailable();
108 void DelegatedFrameResourceCollection::RefResources(
109 const TransferableResourceArray
& resources
) {
110 DCHECK(main_thread_checker_
.CalledOnValidThread());
111 for (size_t i
= 0; i
< resources
.size(); ++i
)
112 resource_id_ref_count_map_
[resources
[i
].id
].refs_to_wait_for
++;
115 static void UnrefResourcesOnImplThread(
116 base::WeakPtr
<DelegatedFrameResourceCollection
> self
,
117 const ReturnedResourceArray
& returned
,
118 BlockingTaskRunner
* main_thread_task_runner
) {
119 main_thread_task_runner
->PostTask(
122 &DelegatedFrameResourceCollection::UnrefResources
, self
, returned
));
126 DelegatedFrameResourceCollection::GetReturnResourcesCallbackForImplThread() {
127 return base::Bind(&UnrefResourcesOnImplThread
,
128 weak_ptr_factory_
.GetWeakPtr());