| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/trace_event/process_memory_dump.h" | 16 #include "base/trace_event/process_memory_dump.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "content/common/view_messages.h" | 18 #include "content/common/view_messages.h" |
| 19 #include "ui/gfx/geometry/size.h" | 19 #include "ui/gfx/geometry/size.h" |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 class BitmapData : public base::RefCountedThreadSafe<BitmapData> { | 23 class BitmapData : public base::RefCountedThreadSafe<BitmapData> { |
| 24 public: | 24 public: |
| 25 BitmapData(base::ProcessHandle process_handle, | 25 explicit BitmapData(size_t buffer_size) : buffer_size(buffer_size) {} |
| 26 size_t buffer_size) | |
| 27 : process_handle(process_handle), | |
| 28 buffer_size(buffer_size) {} | |
| 29 base::ProcessHandle process_handle; | |
| 30 std::unique_ptr<base::SharedMemory> memory; | 26 std::unique_ptr<base::SharedMemory> memory; |
| 31 std::unique_ptr<uint8_t[]> pixels; | 27 std::unique_ptr<uint8_t[]> pixels; |
| 32 size_t buffer_size; | 28 size_t buffer_size; |
| 33 | 29 |
| 34 private: | 30 private: |
| 35 friend class base::RefCountedThreadSafe<BitmapData>; | 31 friend class base::RefCountedThreadSafe<BitmapData>; |
| 36 ~BitmapData() {} | 32 ~BitmapData() {} |
| 37 DISALLOW_COPY_AND_ASSIGN(BitmapData); | 33 DISALLOW_COPY_AND_ASSIGN(BitmapData); |
| 38 }; | 34 }; |
| 39 | 35 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 shared_memory_handle); | 79 shared_memory_handle); |
| 84 if (*shared_memory_handle != base::SharedMemory::NULLHandle()) { | 80 if (*shared_memory_handle != base::SharedMemory::NULLHandle()) { |
| 85 base::AutoLock lock(lock_); | 81 base::AutoLock lock(lock_); |
| 86 owned_bitmaps_.insert(id); | 82 owned_bitmaps_.insert(id); |
| 87 } | 83 } |
| 88 } | 84 } |
| 89 | 85 |
| 90 void HostSharedBitmapManagerClient::ChildAllocatedSharedBitmap( | 86 void HostSharedBitmapManagerClient::ChildAllocatedSharedBitmap( |
| 91 size_t buffer_size, | 87 size_t buffer_size, |
| 92 const base::SharedMemoryHandle& handle, | 88 const base::SharedMemoryHandle& handle, |
| 93 base::ProcessHandle process_handle, | |
| 94 const cc::SharedBitmapId& id) { | 89 const cc::SharedBitmapId& id) { |
| 95 if (manager_->ChildAllocatedSharedBitmap(buffer_size, handle, process_handle, | 90 if (manager_->ChildAllocatedSharedBitmap(buffer_size, handle, id)) { |
| 96 id)) { | |
| 97 base::AutoLock lock(lock_); | 91 base::AutoLock lock(lock_); |
| 98 owned_bitmaps_.insert(id); | 92 owned_bitmaps_.insert(id); |
| 99 } | 93 } |
| 100 } | 94 } |
| 101 | 95 |
| 102 void HostSharedBitmapManagerClient::ChildDeletedSharedBitmap( | 96 void HostSharedBitmapManagerClient::ChildDeletedSharedBitmap( |
| 103 const cc::SharedBitmapId& id) { | 97 const cc::SharedBitmapId& id) { |
| 104 manager_->ChildDeletedSharedBitmap(id); | 98 manager_->ChildDeletedSharedBitmap(id); |
| 105 { | 99 { |
| 106 base::AutoLock lock(lock_); | 100 base::AutoLock lock(lock_); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 117 return g_shared_memory_manager.Pointer(); | 111 return g_shared_memory_manager.Pointer(); |
| 118 } | 112 } |
| 119 | 113 |
| 120 std::unique_ptr<cc::SharedBitmap> HostSharedBitmapManager::AllocateSharedBitmap( | 114 std::unique_ptr<cc::SharedBitmap> HostSharedBitmapManager::AllocateSharedBitmap( |
| 121 const gfx::Size& size) { | 115 const gfx::Size& size) { |
| 122 base::AutoLock lock(lock_); | 116 base::AutoLock lock(lock_); |
| 123 size_t bitmap_size; | 117 size_t bitmap_size; |
| 124 if (!cc::SharedBitmap::SizeInBytes(size, &bitmap_size)) | 118 if (!cc::SharedBitmap::SizeInBytes(size, &bitmap_size)) |
| 125 return std::unique_ptr<cc::SharedBitmap>(); | 119 return std::unique_ptr<cc::SharedBitmap>(); |
| 126 | 120 |
| 127 scoped_refptr<BitmapData> data( | 121 scoped_refptr<BitmapData> data(new BitmapData(bitmap_size)); |
| 128 new BitmapData(base::GetCurrentProcessHandle(), | |
| 129 bitmap_size)); | |
| 130 // Bitmaps allocated in host don't need to be shared to other processes, so | 122 // Bitmaps allocated in host don't need to be shared to other processes, so |
| 131 // allocate them with new instead. | 123 // allocate them with new instead. |
| 132 data->pixels = std::unique_ptr<uint8_t[]>(new uint8_t[bitmap_size]); | 124 data->pixels = std::unique_ptr<uint8_t[]>(new uint8_t[bitmap_size]); |
| 133 | 125 |
| 134 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | 126 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); |
| 135 handle_map_[id] = data; | 127 handle_map_[id] = data; |
| 136 return base::WrapUnique( | 128 return base::WrapUnique( |
| 137 new HostSharedBitmap(data->pixels.get(), data, id, this)); | 129 new HostSharedBitmap(data->pixels.get(), data, id, this)); |
| 138 } | 130 } |
| 139 | 131 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 pmd->CreateSharedGlobalAllocatorDump(guid); | 180 pmd->CreateSharedGlobalAllocatorDump(guid); |
| 189 pmd->AddOwnershipEdge(dump->guid(), guid); | 181 pmd->AddOwnershipEdge(dump->guid(), guid); |
| 190 } | 182 } |
| 191 | 183 |
| 192 return true; | 184 return true; |
| 193 } | 185 } |
| 194 | 186 |
| 195 bool HostSharedBitmapManager::ChildAllocatedSharedBitmap( | 187 bool HostSharedBitmapManager::ChildAllocatedSharedBitmap( |
| 196 size_t buffer_size, | 188 size_t buffer_size, |
| 197 const base::SharedMemoryHandle& handle, | 189 const base::SharedMemoryHandle& handle, |
| 198 base::ProcessHandle process_handle, | |
| 199 const cc::SharedBitmapId& id) { | 190 const cc::SharedBitmapId& id) { |
| 200 base::AutoLock lock(lock_); | 191 base::AutoLock lock(lock_); |
| 201 if (handle_map_.find(id) != handle_map_.end()) | 192 if (handle_map_.find(id) != handle_map_.end()) |
| 202 return false; | 193 return false; |
| 203 scoped_refptr<BitmapData> data( | 194 scoped_refptr<BitmapData> data(new BitmapData(buffer_size)); |
| 204 new BitmapData(process_handle, buffer_size)); | |
| 205 | 195 |
| 206 handle_map_[id] = data; | 196 handle_map_[id] = data; |
| 207 data->memory = base::WrapUnique(new base::SharedMemory(handle, false)); | 197 data->memory = base::WrapUnique(new base::SharedMemory(handle, false)); |
| 208 data->memory->Map(data->buffer_size); | 198 data->memory->Map(data->buffer_size); |
| 209 data->memory->Close(); | 199 data->memory->Close(); |
| 210 return true; | 200 return true; |
| 211 } | 201 } |
| 212 | 202 |
| 213 void HostSharedBitmapManager::AllocateSharedBitmapForChild( | 203 void HostSharedBitmapManager::AllocateSharedBitmapForChild( |
| 214 base::ProcessHandle process_handle, | 204 base::ProcessHandle process_handle, |
| 215 size_t buffer_size, | 205 size_t buffer_size, |
| 216 const cc::SharedBitmapId& id, | 206 const cc::SharedBitmapId& id, |
| 217 base::SharedMemoryHandle* shared_memory_handle) { | 207 base::SharedMemoryHandle* shared_memory_handle) { |
| 218 base::AutoLock lock(lock_); | 208 base::AutoLock lock(lock_); |
| 219 if (handle_map_.find(id) != handle_map_.end()) { | 209 if (handle_map_.find(id) != handle_map_.end()) { |
| 220 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 210 *shared_memory_handle = base::SharedMemory::NULLHandle(); |
| 221 return; | 211 return; |
| 222 } | 212 } |
| 223 std::unique_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); | 213 std::unique_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); |
| 224 if (!shared_memory->CreateAndMapAnonymous(buffer_size)) { | 214 if (!shared_memory->CreateAndMapAnonymous(buffer_size)) { |
| 225 LOG(ERROR) << "Cannot create shared memory buffer"; | 215 LOG(ERROR) << "Cannot create shared memory buffer"; |
| 226 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 216 *shared_memory_handle = base::SharedMemory::NULLHandle(); |
| 227 return; | 217 return; |
| 228 } | 218 } |
| 229 | 219 |
| 230 scoped_refptr<BitmapData> data( | 220 scoped_refptr<BitmapData> data(new BitmapData(buffer_size)); |
| 231 new BitmapData(process_handle, buffer_size)); | |
| 232 data->memory = std::move(shared_memory); | 221 data->memory = std::move(shared_memory); |
| 233 | 222 |
| 234 handle_map_[id] = data; | 223 handle_map_[id] = data; |
| 235 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { | 224 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { |
| 236 LOG(ERROR) << "Cannot share shared memory buffer"; | 225 LOG(ERROR) << "Cannot share shared memory buffer"; |
| 237 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 226 *shared_memory_handle = base::SharedMemory::NULLHandle(); |
| 238 return; | 227 return; |
| 239 } | 228 } |
| 240 data->memory->Close(); | 229 data->memory->Close(); |
| 241 } | 230 } |
| 242 | 231 |
| 243 void HostSharedBitmapManager::ChildDeletedSharedBitmap( | 232 void HostSharedBitmapManager::ChildDeletedSharedBitmap( |
| 244 const cc::SharedBitmapId& id) { | 233 const cc::SharedBitmapId& id) { |
| 245 base::AutoLock lock(lock_); | 234 base::AutoLock lock(lock_); |
| 246 handle_map_.erase(id); | 235 handle_map_.erase(id); |
| 247 } | 236 } |
| 248 | 237 |
| 249 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { | 238 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { |
| 250 base::AutoLock lock(lock_); | 239 base::AutoLock lock(lock_); |
| 251 return handle_map_.size(); | 240 return handle_map_.size(); |
| 252 } | 241 } |
| 253 | 242 |
| 254 void HostSharedBitmapManager::FreeSharedMemoryFromMap( | 243 void HostSharedBitmapManager::FreeSharedMemoryFromMap( |
| 255 const cc::SharedBitmapId& id) { | 244 const cc::SharedBitmapId& id) { |
| 256 base::AutoLock lock(lock_); | 245 base::AutoLock lock(lock_); |
| 257 handle_map_.erase(id); | 246 handle_map_.erase(id); |
| 258 } | 247 } |
| 259 | 248 |
| 260 } // namespace content | 249 } // namespace content |
| OLD | NEW |