| 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/bindings/strong_binding.h" |
| 20 #include "mojo/public/cpp/system/platform_handle.h" |
| 19 #include "ui/gfx/geometry/size.h" | 21 #include "ui/gfx/geometry/size.h" |
| 20 | 22 |
| 21 namespace content { | 23 namespace display_compositor { |
| 22 | 24 |
| 23 class BitmapData : public base::RefCountedThreadSafe<BitmapData> { | 25 class BitmapData : public base::RefCountedThreadSafe<BitmapData> { |
| 24 public: | 26 public: |
| 25 explicit BitmapData(size_t buffer_size) : buffer_size(buffer_size) {} | 27 explicit BitmapData(size_t buffer_size) : buffer_size(buffer_size) {} |
| 26 std::unique_ptr<base::SharedMemory> memory; | 28 std::unique_ptr<base::SharedMemory> memory; |
| 27 std::unique_ptr<uint8_t[]> pixels; | 29 std::unique_ptr<uint8_t[]> pixels; |
| 28 size_t buffer_size; | 30 size_t buffer_size; |
| 29 | 31 |
| 30 private: | 32 private: |
| 31 friend class base::RefCountedThreadSafe<BitmapData>; | 33 friend class base::RefCountedThreadSafe<BitmapData>; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 50 manager_->FreeSharedMemoryFromMap(id()); | 52 manager_->FreeSharedMemoryFromMap(id()); |
| 51 } | 53 } |
| 52 | 54 |
| 53 private: | 55 private: |
| 54 scoped_refptr<BitmapData> bitmap_data_; | 56 scoped_refptr<BitmapData> bitmap_data_; |
| 55 HostSharedBitmapManager* manager_; | 57 HostSharedBitmapManager* manager_; |
| 56 }; | 58 }; |
| 57 | 59 |
| 58 } // namespace | 60 } // namespace |
| 59 | 61 |
| 62 class MojoSharedBitmapManagerImpl : public mojom::SharedBitmapManager { |
| 63 public: |
| 64 MojoSharedBitmapManagerImpl( |
| 65 HostSharedBitmapManagerClient* bitmap_manager_client) |
| 66 : bitmap_manager_client_(bitmap_manager_client) {} |
| 67 |
| 68 // mojom::SharedBitmapManager overrides: |
| 69 void AllocatedSharedBitmap(mojo::ScopedSharedBufferHandle buffer, |
| 70 const cc::SharedBitmapId& id) override { |
| 71 base::SharedMemoryHandle memory_handle; |
| 72 size_t size; |
| 73 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 74 std::move(buffer), &memory_handle, &size, NULL); |
| 75 DCHECK_EQ(result, MOJO_RESULT_OK); |
| 76 bitmap_manager_client_->ChildAllocatedSharedBitmap(size, memory_handle, id); |
| 77 } |
| 78 |
| 79 void DeletedSharedBitmap(const cc::SharedBitmapId& id) override { |
| 80 bitmap_manager_client_->ChildDeletedSharedBitmap(id); |
| 81 } |
| 82 |
| 83 private: |
| 84 HostSharedBitmapManagerClient* bitmap_manager_client_; |
| 85 }; |
| 86 |
| 60 base::LazyInstance<HostSharedBitmapManager> g_shared_memory_manager = | 87 base::LazyInstance<HostSharedBitmapManager> g_shared_memory_manager = |
| 61 LAZY_INSTANCE_INITIALIZER; | 88 LAZY_INSTANCE_INITIALIZER; |
| 62 | 89 |
| 63 HostSharedBitmapManagerClient::HostSharedBitmapManagerClient( | 90 HostSharedBitmapManagerClient::HostSharedBitmapManagerClient( |
| 64 HostSharedBitmapManager* manager) | 91 HostSharedBitmapManager* manager) |
| 65 : manager_(manager) { | 92 : manager_(manager) {} |
| 66 } | |
| 67 | 93 |
| 68 HostSharedBitmapManagerClient::~HostSharedBitmapManagerClient() { | 94 HostSharedBitmapManagerClient::~HostSharedBitmapManagerClient() { |
| 69 for (const auto& id : owned_bitmaps_) | 95 for (const auto& id : owned_bitmaps_) |
| 70 manager_->ChildDeletedSharedBitmap(id); | 96 manager_->ChildDeletedSharedBitmap(id); |
| 71 } | 97 } |
| 72 | 98 |
| 99 void HostSharedBitmapManagerClient::Bind( |
| 100 mojom::SharedBitmapManagerRequest request) { |
| 101 mojo::MakeStrongBinding(base::MakeUnique<MojoSharedBitmapManagerImpl>(this), |
| 102 std::move(request)); |
| 103 } |
| 104 |
| 73 void HostSharedBitmapManagerClient::AllocateSharedBitmapForChild( | 105 void HostSharedBitmapManagerClient::AllocateSharedBitmapForChild( |
| 74 base::ProcessHandle process_handle, | 106 base::ProcessHandle process_handle, |
| 75 size_t buffer_size, | 107 size_t buffer_size, |
| 76 const cc::SharedBitmapId& id, | 108 const cc::SharedBitmapId& id, |
| 77 base::SharedMemoryHandle* shared_memory_handle) { | 109 base::SharedMemoryHandle* shared_memory_handle) { |
| 78 manager_->AllocateSharedBitmapForChild(process_handle, buffer_size, id, | 110 manager_->AllocateSharedBitmapForChild(process_handle, buffer_size, id, |
| 79 shared_memory_handle); | 111 shared_memory_handle); |
| 80 if (*shared_memory_handle != base::SharedMemory::NULLHandle()) { | 112 if (*shared_memory_handle != base::SharedMemory::NULLHandle()) { |
| 81 base::AutoLock lock(lock_); | 113 base::AutoLock lock(lock_); |
| 82 owned_bitmaps_.insert(id); | 114 owned_bitmaps_.insert(id); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 | 250 |
| 219 scoped_refptr<BitmapData> data(new BitmapData(buffer_size)); | 251 scoped_refptr<BitmapData> data(new BitmapData(buffer_size)); |
| 220 data->memory = std::move(shared_memory); | 252 data->memory = std::move(shared_memory); |
| 221 | 253 |
| 222 handle_map_[id] = data; | 254 handle_map_[id] = data; |
| 223 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { | 255 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { |
| 224 LOG(ERROR) << "Cannot share shared memory buffer"; | 256 LOG(ERROR) << "Cannot share shared memory buffer"; |
| 225 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 257 *shared_memory_handle = base::SharedMemory::NULLHandle(); |
| 226 return; | 258 return; |
| 227 } | 259 } |
| 228 data->memory->Close(); | 260 data->memory->Close(); |
| 229 } | 261 } |
| 230 | 262 |
| 231 void HostSharedBitmapManager::ChildDeletedSharedBitmap( | 263 void HostSharedBitmapManager::ChildDeletedSharedBitmap( |
| 232 const cc::SharedBitmapId& id) { | 264 const cc::SharedBitmapId& id) { |
| 233 base::AutoLock lock(lock_); | 265 base::AutoLock lock(lock_); |
| 234 handle_map_.erase(id); | 266 handle_map_.erase(id); |
| 235 } | 267 } |
| 236 | 268 |
| 237 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { | 269 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { |
| 238 base::AutoLock lock(lock_); | 270 base::AutoLock lock(lock_); |
| 239 return handle_map_.size(); | 271 return handle_map_.size(); |
| 240 } | 272 } |
| 241 | 273 |
| 242 void HostSharedBitmapManager::FreeSharedMemoryFromMap( | 274 void HostSharedBitmapManager::FreeSharedMemoryFromMap( |
| 243 const cc::SharedBitmapId& id) { | 275 const cc::SharedBitmapId& id) { |
| 244 base::AutoLock lock(lock_); | 276 base::AutoLock lock(lock_); |
| 245 handle_map_.erase(id); | 277 handle_map_.erase(id); |
| 246 } | 278 } |
| 247 | 279 |
| 248 } // namespace content | 280 } // namespace display_compositor |
| OLD | NEW |