OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/host_shared_bitmap_manager.h" | 5 #include "content/common/host_shared_bitmap_manager.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.h" |
10 #include "ui/gfx/geometry/size.h" | 10 #include "ui/gfx/geometry/size.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 private: | 48 private: |
49 scoped_refptr<BitmapData> bitmap_data_; | 49 scoped_refptr<BitmapData> bitmap_data_; |
50 HostSharedBitmapManager* manager_; | 50 HostSharedBitmapManager* manager_; |
51 }; | 51 }; |
52 | 52 |
53 } // namespace | 53 } // namespace |
54 | 54 |
55 base::LazyInstance<HostSharedBitmapManager> g_shared_memory_manager = | 55 base::LazyInstance<HostSharedBitmapManager> g_shared_memory_manager = |
56 LAZY_INSTANCE_INITIALIZER; | 56 LAZY_INSTANCE_INITIALIZER; |
57 | 57 |
| 58 HostSharedBitmapManagerClient::HostSharedBitmapManagerClient( |
| 59 HostSharedBitmapManager* manager) |
| 60 : manager_(manager) { |
| 61 } |
| 62 |
| 63 HostSharedBitmapManagerClient::~HostSharedBitmapManagerClient() { |
| 64 for (const auto& id : owned_bitmaps_) |
| 65 manager_->ChildDeletedSharedBitmap(id); |
| 66 } |
| 67 |
| 68 void HostSharedBitmapManagerClient::AllocateSharedBitmapForChild( |
| 69 base::ProcessHandle process_handle, |
| 70 size_t buffer_size, |
| 71 const cc::SharedBitmapId& id, |
| 72 base::SharedMemoryHandle* shared_memory_handle) { |
| 73 manager_->AllocateSharedBitmapForChild(process_handle, buffer_size, id, |
| 74 shared_memory_handle); |
| 75 owned_bitmaps_.insert(id); |
| 76 } |
| 77 |
| 78 void HostSharedBitmapManagerClient::ChildAllocatedSharedBitmap( |
| 79 size_t buffer_size, |
| 80 const base::SharedMemoryHandle& handle, |
| 81 base::ProcessHandle process_handle, |
| 82 const cc::SharedBitmapId& id) { |
| 83 manager_->ChildAllocatedSharedBitmap(buffer_size, handle, process_handle, id); |
| 84 owned_bitmaps_.insert(id); |
| 85 } |
| 86 |
| 87 void HostSharedBitmapManagerClient::ChildDeletedSharedBitmap( |
| 88 const cc::SharedBitmapId& id) { |
| 89 manager_->ChildDeletedSharedBitmap(id); |
| 90 owned_bitmaps_.erase(id); |
| 91 } |
| 92 |
58 HostSharedBitmapManager::HostSharedBitmapManager() {} | 93 HostSharedBitmapManager::HostSharedBitmapManager() {} |
59 HostSharedBitmapManager::~HostSharedBitmapManager() { | 94 HostSharedBitmapManager::~HostSharedBitmapManager() { |
60 DCHECK(handle_map_.empty()); | 95 DCHECK(handle_map_.empty()); |
61 } | 96 } |
62 | 97 |
63 HostSharedBitmapManager* HostSharedBitmapManager::current() { | 98 HostSharedBitmapManager* HostSharedBitmapManager::current() { |
64 return g_shared_memory_manager.Pointer(); | 99 return g_shared_memory_manager.Pointer(); |
65 } | 100 } |
66 | 101 |
67 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::AllocateSharedBitmap( | 102 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::AllocateSharedBitmap( |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 const base::SharedMemoryHandle& handle, | 151 const base::SharedMemoryHandle& handle, |
117 base::ProcessHandle process_handle, | 152 base::ProcessHandle process_handle, |
118 const cc::SharedBitmapId& id) { | 153 const cc::SharedBitmapId& id) { |
119 base::AutoLock lock(lock_); | 154 base::AutoLock lock(lock_); |
120 if (handle_map_.find(id) != handle_map_.end()) | 155 if (handle_map_.find(id) != handle_map_.end()) |
121 return; | 156 return; |
122 scoped_refptr<BitmapData> data( | 157 scoped_refptr<BitmapData> data( |
123 new BitmapData(process_handle, buffer_size)); | 158 new BitmapData(process_handle, buffer_size)); |
124 | 159 |
125 handle_map_[id] = data; | 160 handle_map_[id] = data; |
126 process_map_[process_handle].insert(id); | |
127 #if defined(OS_WIN) | 161 #if defined(OS_WIN) |
128 data->memory = make_scoped_ptr( | 162 data->memory = make_scoped_ptr( |
129 new base::SharedMemory(handle, false, data->process_handle)); | 163 new base::SharedMemory(handle, false, data->process_handle)); |
130 #else | 164 #else |
131 data->memory = | 165 data->memory = |
132 make_scoped_ptr(new base::SharedMemory(handle, false)); | 166 make_scoped_ptr(new base::SharedMemory(handle, false)); |
133 #endif | 167 #endif |
134 data->memory->Map(data->buffer_size); | 168 data->memory->Map(data->buffer_size); |
135 data->memory->Close(); | 169 data->memory->Close(); |
136 } | 170 } |
(...skipping 13 matching lines...) Expand all Loading... |
150 LOG(ERROR) << "Cannot create shared memory buffer"; | 184 LOG(ERROR) << "Cannot create shared memory buffer"; |
151 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 185 *shared_memory_handle = base::SharedMemory::NULLHandle(); |
152 return; | 186 return; |
153 } | 187 } |
154 | 188 |
155 scoped_refptr<BitmapData> data( | 189 scoped_refptr<BitmapData> data( |
156 new BitmapData(process_handle, buffer_size)); | 190 new BitmapData(process_handle, buffer_size)); |
157 data->memory = shared_memory.Pass(); | 191 data->memory = shared_memory.Pass(); |
158 | 192 |
159 handle_map_[id] = data; | 193 handle_map_[id] = data; |
160 process_map_[process_handle].insert(id); | |
161 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { | 194 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { |
162 LOG(ERROR) << "Cannot share shared memory buffer"; | 195 LOG(ERROR) << "Cannot share shared memory buffer"; |
163 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 196 *shared_memory_handle = base::SharedMemory::NULLHandle(); |
164 return; | 197 return; |
165 } | 198 } |
166 data->memory->Close(); | 199 data->memory->Close(); |
167 } | 200 } |
168 | 201 |
169 void HostSharedBitmapManager::ChildDeletedSharedBitmap( | 202 void HostSharedBitmapManager::ChildDeletedSharedBitmap( |
170 const cc::SharedBitmapId& id) { | 203 const cc::SharedBitmapId& id) { |
171 base::AutoLock lock(lock_); | 204 base::AutoLock lock(lock_); |
172 BitmapMap::iterator it = handle_map_.find(id); | 205 handle_map_.erase(id); |
173 if (it == handle_map_.end()) | |
174 return; | |
175 base::hash_set<cc::SharedBitmapId>& res = | |
176 process_map_[it->second->process_handle]; | |
177 res.erase(id); | |
178 handle_map_.erase(it); | |
179 } | |
180 | |
181 void HostSharedBitmapManager::ProcessRemoved( | |
182 base::ProcessHandle process_handle) { | |
183 base::AutoLock lock(lock_); | |
184 ProcessMap::iterator proc_it = process_map_.find(process_handle); | |
185 if (proc_it == process_map_.end()) | |
186 return; | |
187 base::hash_set<cc::SharedBitmapId>& res = proc_it->second; | |
188 | |
189 for (base::hash_set<cc::SharedBitmapId>::iterator it = res.begin(); | |
190 it != res.end(); | |
191 ++it) { | |
192 handle_map_.erase(*it); | |
193 } | |
194 process_map_.erase(proc_it); | |
195 } | 206 } |
196 | 207 |
197 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { | 208 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { |
198 base::AutoLock lock(lock_); | 209 base::AutoLock lock(lock_); |
199 return handle_map_.size(); | 210 return handle_map_.size(); |
200 } | 211 } |
201 | 212 |
202 void HostSharedBitmapManager::FreeSharedMemoryFromMap( | 213 void HostSharedBitmapManager::FreeSharedMemoryFromMap( |
203 const cc::SharedBitmapId& id) { | 214 const cc::SharedBitmapId& id) { |
204 base::AutoLock lock(lock_); | 215 base::AutoLock lock(lock_); |
205 handle_map_.erase(id); | 216 handle_map_.erase(id); |
206 } | 217 } |
207 | 218 |
208 } // namespace content | 219 } // namespace content |
OLD | NEW |