[GCM] Investigatory CHECKs for crash in parsing stream
[chromium-blink-merge.git] / cc / layers / delegated_frame_resource_collection.cc
blobb0e4cbf72964f6993251828a8ffd95a7900a7c30
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_(NULL),
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) {
25 client_ = 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())
41 return false;
43 ReturnedResourceArray to_return;
45 for (ResourceIdRefCountMap::iterator it = resource_id_ref_count_map_.begin();
46 it != resource_id_ref_count_map_.end();
47 ++it) {
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;
53 returned.lost = true;
54 to_return.push_back(returned);
57 returned_resources_for_child_compositor_.insert(
58 returned_resources_for_child_compositor_.end(),
59 to_return.begin(),
60 to_return.end());
61 if (client_)
62 client_->UnusedResourcesAreAvailable();
63 return true;
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_)
80 return;
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())
98 return;
100 returned_resources_for_child_compositor_.insert(
101 returned_resources_for_child_compositor_.end(),
102 to_return.begin(),
103 to_return.end());
104 if (client_)
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(
120 FROM_HERE,
121 base::Bind(
122 &DelegatedFrameResourceCollection::UnrefResources, self, returned));
125 ReturnCallback
126 DelegatedFrameResourceCollection::GetReturnResourcesCallbackForImplThread() {
127 return base::Bind(&UnrefResourcesOnImplThread,
128 weak_ptr_factory_.GetWeakPtr());
131 } // namespace cc