| 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 "components/display_compositor/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/strings/stringprintf.h" |
| 16 #include "base/trace_event/process_memory_dump.h" | 17 #include "base/trace_event/process_memory_dump.h" |
| 17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 18 #include "content/common/view_messages.h" | 19 #include "mojo/public/cpp/system/platform_handle.h" |
| 19 #include "ui/gfx/geometry/size.h" | 20 #include "ui/gfx/geometry/size.h" |
| 20 | 21 |
| 21 namespace content { | 22 namespace display_compositor { |
| 22 | 23 |
| 23 class BitmapData : public base::RefCountedThreadSafe<BitmapData> { | 24 class BitmapData : public base::RefCountedThreadSafe<BitmapData> { |
| 24 public: | 25 public: |
| 25 explicit BitmapData(size_t buffer_size) : buffer_size(buffer_size) {} | 26 explicit BitmapData(size_t buffer_size) : buffer_size(buffer_size) {} |
| 26 std::unique_ptr<base::SharedMemory> memory; | 27 std::unique_ptr<base::SharedMemory> memory; |
| 27 std::unique_ptr<uint8_t[]> pixels; | 28 std::unique_ptr<uint8_t[]> pixels; |
| 28 size_t buffer_size; | 29 size_t buffer_size; |
| 29 | 30 |
| 30 private: | 31 private: |
| 31 friend class base::RefCountedThreadSafe<BitmapData>; | 32 friend class base::RefCountedThreadSafe<BitmapData>; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 55 HostSharedBitmapManager* manager_; | 56 HostSharedBitmapManager* manager_; |
| 56 }; | 57 }; |
| 57 | 58 |
| 58 } // namespace | 59 } // namespace |
| 59 | 60 |
| 60 base::LazyInstance<HostSharedBitmapManager>::DestructorAtExit | 61 base::LazyInstance<HostSharedBitmapManager>::DestructorAtExit |
| 61 g_shared_memory_manager = LAZY_INSTANCE_INITIALIZER; | 62 g_shared_memory_manager = LAZY_INSTANCE_INITIALIZER; |
| 62 | 63 |
| 63 HostSharedBitmapManagerClient::HostSharedBitmapManagerClient( | 64 HostSharedBitmapManagerClient::HostSharedBitmapManagerClient( |
| 64 HostSharedBitmapManager* manager) | 65 HostSharedBitmapManager* manager) |
| 65 : manager_(manager) { | 66 : manager_(manager), binding_(this) {} |
| 66 } | |
| 67 | 67 |
| 68 HostSharedBitmapManagerClient::~HostSharedBitmapManagerClient() { | 68 HostSharedBitmapManagerClient::~HostSharedBitmapManagerClient() { |
| 69 for (const auto& id : owned_bitmaps_) | 69 for (const auto& id : owned_bitmaps_) |
| 70 manager_->ChildDeletedSharedBitmap(id); | 70 manager_->ChildDeletedSharedBitmap(id); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void HostSharedBitmapManagerClient::Bind( |
| 74 cc::mojom::SharedBitmapManagerAssociatedRequest request) { |
| 75 binding_.Bind(std::move(request)); |
| 76 } |
| 77 |
| 78 void HostSharedBitmapManagerClient::DidAllocateSharedBitmap( |
| 79 mojo::ScopedSharedBufferHandle buffer, |
| 80 const cc::SharedBitmapId& id) { |
| 81 base::SharedMemoryHandle memory_handle; |
| 82 size_t size; |
| 83 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 84 std::move(buffer), &memory_handle, &size, NULL); |
| 85 DCHECK_EQ(result, MOJO_RESULT_OK); |
| 86 this->ChildAllocatedSharedBitmap(size, memory_handle, id); |
| 87 } |
| 88 |
| 73 void HostSharedBitmapManagerClient::AllocateSharedBitmapForChild( | 89 void HostSharedBitmapManagerClient::AllocateSharedBitmapForChild( |
| 74 base::ProcessHandle process_handle, | 90 base::ProcessHandle process_handle, |
| 75 size_t buffer_size, | 91 size_t buffer_size, |
| 76 const cc::SharedBitmapId& id, | 92 const cc::SharedBitmapId& id, |
| 77 base::SharedMemoryHandle* shared_memory_handle) { | 93 base::SharedMemoryHandle* shared_memory_handle) { |
| 78 manager_->AllocateSharedBitmapForChild(process_handle, buffer_size, id, | 94 manager_->AllocateSharedBitmapForChild(process_handle, buffer_size, id, |
| 79 shared_memory_handle); | 95 shared_memory_handle); |
| 80 if (*shared_memory_handle != base::SharedMemory::NULLHandle()) { | 96 if (*shared_memory_handle != base::SharedMemory::NULLHandle()) { |
| 81 base::AutoLock lock(lock_); | 97 base::AutoLock lock(lock_); |
| 82 owned_bitmaps_.insert(id); | 98 owned_bitmaps_.insert(id); |
| 83 } | 99 } |
| 84 } | 100 } |
| 85 | 101 |
| 86 void HostSharedBitmapManagerClient::ChildAllocatedSharedBitmap( | 102 void HostSharedBitmapManagerClient::ChildAllocatedSharedBitmap( |
| 87 size_t buffer_size, | 103 size_t buffer_size, |
| 88 const base::SharedMemoryHandle& handle, | 104 const base::SharedMemoryHandle& handle, |
| 89 const cc::SharedBitmapId& id) { | 105 const cc::SharedBitmapId& id) { |
| 90 if (manager_->ChildAllocatedSharedBitmap(buffer_size, handle, id)) { | 106 if (manager_->ChildAllocatedSharedBitmap(buffer_size, handle, id)) { |
| 91 base::AutoLock lock(lock_); | 107 base::AutoLock lock(lock_); |
| 92 owned_bitmaps_.insert(id); | 108 owned_bitmaps_.insert(id); |
| 93 } | 109 } |
| 94 } | 110 } |
| 95 | 111 |
| 96 void HostSharedBitmapManagerClient::ChildDeletedSharedBitmap( | 112 void HostSharedBitmapManagerClient::DidDeleteSharedBitmap( |
| 97 const cc::SharedBitmapId& id) { | 113 const cc::SharedBitmapId& id) { |
| 98 manager_->ChildDeletedSharedBitmap(id); | 114 manager_->ChildDeletedSharedBitmap(id); |
| 99 { | 115 { |
| 100 base::AutoLock lock(lock_); | 116 base::AutoLock lock(lock_); |
| 101 owned_bitmaps_.erase(id); | 117 owned_bitmaps_.erase(id); |
| 102 } | 118 } |
| 103 } | 119 } |
| 104 | 120 |
| 105 HostSharedBitmapManager::HostSharedBitmapManager() {} | 121 HostSharedBitmapManager::HostSharedBitmapManager() {} |
| 106 HostSharedBitmapManager::~HostSharedBitmapManager() { | 122 HostSharedBitmapManager::~HostSharedBitmapManager() { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 | 234 |
| 219 scoped_refptr<BitmapData> data(new BitmapData(buffer_size)); | 235 scoped_refptr<BitmapData> data(new BitmapData(buffer_size)); |
| 220 data->memory = std::move(shared_memory); | 236 data->memory = std::move(shared_memory); |
| 221 | 237 |
| 222 handle_map_[id] = data; | 238 handle_map_[id] = data; |
| 223 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { | 239 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { |
| 224 LOG(ERROR) << "Cannot share shared memory buffer"; | 240 LOG(ERROR) << "Cannot share shared memory buffer"; |
| 225 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 241 *shared_memory_handle = base::SharedMemory::NULLHandle(); |
| 226 return; | 242 return; |
| 227 } | 243 } |
| 228 data->memory->Close(); | 244 data->memory->Close(); |
| 229 } | 245 } |
| 230 | 246 |
| 231 void HostSharedBitmapManager::ChildDeletedSharedBitmap( | 247 void HostSharedBitmapManager::ChildDeletedSharedBitmap( |
| 232 const cc::SharedBitmapId& id) { | 248 const cc::SharedBitmapId& id) { |
| 233 base::AutoLock lock(lock_); | 249 base::AutoLock lock(lock_); |
| 234 handle_map_.erase(id); | 250 handle_map_.erase(id); |
| 235 } | 251 } |
| 236 | 252 |
| 237 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { | 253 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { |
| 238 base::AutoLock lock(lock_); | 254 base::AutoLock lock(lock_); |
| 239 return handle_map_.size(); | 255 return handle_map_.size(); |
| 240 } | 256 } |
| 241 | 257 |
| 242 void HostSharedBitmapManager::FreeSharedMemoryFromMap( | 258 void HostSharedBitmapManager::FreeSharedMemoryFromMap( |
| 243 const cc::SharedBitmapId& id) { | 259 const cc::SharedBitmapId& id) { |
| 244 base::AutoLock lock(lock_); | 260 base::AutoLock lock(lock_); |
| 245 handle_map_.erase(id); | 261 handle_map_.erase(id); |
| 246 } | 262 } |
| 247 | 263 |
| 248 } // namespace content | 264 } // namespace display_compositor |
| OLD | NEW |