| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/child/child_shared_bitmap_manager.h" | 5 #include "services/ui/public/cpp/bitmap/child_shared_bitmap_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/process/memory.h" | 13 #include "base/process/memory.h" |
| 14 #include "base/process/process_metrics.h" | 14 #include "base/process/process_metrics.h" |
| 15 #include "base/trace_event/trace_event.h" |
| 15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 16 #include "content/child/child_thread_impl.h" | |
| 17 #include "content/common/child_process_messages.h" | |
| 18 #include "mojo/public/cpp/system/platform_handle.h" | 17 #include "mojo/public/cpp/system/platform_handle.h" |
| 19 #include "ui/gfx/geometry/size.h" | 18 #include "ui/gfx/geometry/size.h" |
| 20 | 19 |
| 21 namespace content { | 20 namespace ui { |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 class ChildSharedBitmap : public cc::SharedBitmap { | 24 class ChildSharedBitmap : public cc::SharedBitmap { |
| 26 public: | 25 public: |
| 27 ChildSharedBitmap( | 26 ChildSharedBitmap( |
| 28 const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>& | 27 const scoped_refptr<cc::mojom::ThreadSafeSharedBitmapManagerAssociatedPtr> |
| 29 render_message_filter_ptr, | 28 shared_bitmap_manager_ptr, |
| 30 base::SharedMemory* shared_memory, | 29 base::SharedMemory* shared_memory, |
| 31 const cc::SharedBitmapId& id) | 30 const cc::SharedBitmapId& id) |
| 32 : cc::SharedBitmap(static_cast<uint8_t*>(shared_memory->memory()), id), | 31 : cc::SharedBitmap(static_cast<uint8_t*>(shared_memory->memory()), id), |
| 33 render_message_filter_ptr_(render_message_filter_ptr) {} | 32 shared_bitmap_manager_ptr_(shared_bitmap_manager_ptr) {} |
| 34 | 33 |
| 35 ChildSharedBitmap( | 34 ChildSharedBitmap( |
| 36 const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>& | 35 const scoped_refptr<cc::mojom::ThreadSafeSharedBitmapManagerAssociatedPtr> |
| 37 render_message_filter_ptr, | 36 shared_bitmap_manager_ptr, |
| 38 std::unique_ptr<base::SharedMemory> shared_memory_holder, | 37 std::unique_ptr<base::SharedMemory> shared_memory_holder, |
| 39 const cc::SharedBitmapId& id) | 38 const cc::SharedBitmapId& id) |
| 40 : ChildSharedBitmap(render_message_filter_ptr, | 39 : ChildSharedBitmap(shared_bitmap_manager_ptr, |
| 41 shared_memory_holder.get(), | 40 shared_memory_holder.get(), |
| 42 id) { | 41 id) { |
| 43 shared_memory_holder_ = std::move(shared_memory_holder); | 42 shared_memory_holder_ = std::move(shared_memory_holder); |
| 44 } | 43 } |
| 45 | 44 |
| 46 ~ChildSharedBitmap() override { | 45 ~ChildSharedBitmap() override { |
| 47 (*render_message_filter_ptr_)->DeletedSharedBitmap(id()); | 46 (*shared_bitmap_manager_ptr_)->DidDeleteSharedBitmap(id()); |
| 48 } | 47 } |
| 49 | 48 |
| 50 private: | 49 private: |
| 51 scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr> | 50 scoped_refptr<cc::mojom::ThreadSafeSharedBitmapManagerAssociatedPtr> |
| 52 render_message_filter_ptr_; | 51 shared_bitmap_manager_ptr_; |
| 53 std::unique_ptr<base::SharedMemory> shared_memory_holder_; | 52 std::unique_ptr<base::SharedMemory> shared_memory_holder_; |
| 54 }; | 53 }; |
| 55 | 54 |
| 56 // Collect extra information for debugging bitmap creation failures. | 55 // Collect extra information for debugging bitmap creation failures. |
| 57 void CollectMemoryUsageAndDie(const gfx::Size& size, size_t alloc_size) { | 56 void CollectMemoryUsageAndDie(const gfx::Size& size, size_t alloc_size) { |
| 58 #if defined(OS_WIN) | 57 #if defined(OS_WIN) |
| 59 int width = size.width(); | 58 int width = size.width(); |
| 60 int height = size.height(); | 59 int height = size.height(); |
| 61 DWORD last_error = GetLastError(); | 60 DWORD last_error = GetLastError(); |
| 62 | 61 |
| 63 std::unique_ptr<base::ProcessMetrics> metrics( | 62 std::unique_ptr<base::ProcessMetrics> metrics( |
| 64 base::ProcessMetrics::CreateProcessMetrics( | 63 base::ProcessMetrics::CreateProcessMetrics( |
| 65 base::GetCurrentProcessHandle())); | 64 base::GetCurrentProcessHandle())); |
| 66 | 65 |
| 67 size_t private_bytes = 0; | 66 size_t private_bytes = 0; |
| 68 size_t shared_bytes = 0; | 67 size_t shared_bytes = 0; |
| 69 metrics->GetMemoryBytes(&private_bytes, &shared_bytes); | 68 metrics->GetMemoryBytes(&private_bytes, &shared_bytes); |
| 70 | 69 |
| 71 base::debug::Alias(&width); | 70 base::debug::Alias(&width); |
| 72 base::debug::Alias(&height); | 71 base::debug::Alias(&height); |
| 73 base::debug::Alias(&last_error); | 72 base::debug::Alias(&last_error); |
| 74 base::debug::Alias(&private_bytes); | 73 base::debug::Alias(&private_bytes); |
| 75 base::debug::Alias(&shared_bytes); | 74 base::debug::Alias(&shared_bytes); |
| 76 #endif | 75 #endif |
| 77 base::TerminateBecauseOutOfMemory(alloc_size); | 76 base::TerminateBecauseOutOfMemory(alloc_size); |
| 78 } | 77 } |
| 79 | 78 |
| 79 // Allocates a block of shared memory of the given size. Returns nullptr on |
| 80 // failure. |
| 81 std::unique_ptr<base::SharedMemory> AllocateSharedMemory(size_t buf_size) { |
| 82 mojo::ScopedSharedBufferHandle mojo_buf = |
| 83 mojo::SharedBufferHandle::Create(buf_size); |
| 84 if (!mojo_buf->is_valid()) { |
| 85 LOG(WARNING) << "Browser failed to allocate shared memory"; |
| 86 return nullptr; |
| 87 } |
| 88 |
| 89 base::SharedMemoryHandle shared_buf; |
| 90 if (mojo::UnwrapSharedMemoryHandle(std::move(mojo_buf), &shared_buf, nullptr, |
| 91 nullptr) != MOJO_RESULT_OK) { |
| 92 LOG(WARNING) << "Browser failed to allocate shared memory"; |
| 93 return nullptr; |
| 94 } |
| 95 |
| 96 return base::MakeUnique<base::SharedMemory>(shared_buf, false); |
| 97 } |
| 98 |
| 80 } // namespace | 99 } // namespace |
| 81 | 100 |
| 82 ChildSharedBitmapManager::ChildSharedBitmapManager( | 101 ChildSharedBitmapManager::ChildSharedBitmapManager( |
| 83 const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>& | 102 const scoped_refptr<cc::mojom::ThreadSafeSharedBitmapManagerAssociatedPtr>& |
| 84 render_message_filter_ptr) | 103 shared_bitmap_manager_ptr) |
| 85 : render_message_filter_ptr_(render_message_filter_ptr) {} | 104 : shared_bitmap_manager_ptr_(shared_bitmap_manager_ptr) {} |
| 86 | 105 |
| 87 ChildSharedBitmapManager::~ChildSharedBitmapManager() {} | 106 ChildSharedBitmapManager::~ChildSharedBitmapManager() {} |
| 88 | 107 |
| 89 std::unique_ptr<cc::SharedBitmap> | 108 std::unique_ptr<cc::SharedBitmap> |
| 90 ChildSharedBitmapManager::AllocateSharedBitmap(const gfx::Size& size) { | 109 ChildSharedBitmapManager::AllocateSharedBitmap(const gfx::Size& size) { |
| 91 TRACE_EVENT2("renderer", "ChildSharedBitmapManager::AllocateSharedBitmap", | 110 TRACE_EVENT2("renderer", "ChildSharedBitmapManager::AllocateSharedBitmap", |
| 92 "width", size.width(), "height", size.height()); | 111 "width", size.width(), "height", size.height()); |
| 93 size_t memory_size; | 112 size_t memory_size; |
| 94 if (!cc::SharedBitmap::SizeInBytes(size, &memory_size)) | 113 if (!cc::SharedBitmap::SizeInBytes(size, &memory_size)) |
| 95 return nullptr; | 114 return nullptr; |
| 96 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | 115 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); |
| 97 std::unique_ptr<base::SharedMemory> memory = | 116 std::unique_ptr<base::SharedMemory> memory = |
| 98 ChildThreadImpl::AllocateSharedMemory(memory_size); | 117 AllocateSharedMemory(memory_size); |
| 99 if (!memory || !memory->Map(memory_size)) | 118 if (!memory || !memory->Map(memory_size)) |
| 100 CollectMemoryUsageAndDie(size, memory_size); | 119 CollectMemoryUsageAndDie(size, memory_size); |
| 101 | 120 |
| 102 NotifyAllocatedSharedBitmap(memory.get(), id); | 121 NotifyAllocatedSharedBitmap(memory.get(), id); |
| 103 | 122 |
| 104 // Close the associated FD to save resources, the previously mapped memory | 123 // Close the associated FD to save resources, the previously mapped memory |
| 105 // remains available. | 124 // remains available. |
| 106 memory->Close(); | 125 memory->Close(); |
| 107 | 126 |
| 108 return base::MakeUnique<ChildSharedBitmap>(render_message_filter_ptr_, | 127 return base::MakeUnique<ChildSharedBitmap>(shared_bitmap_manager_ptr_, |
| 109 std::move(memory), id); | 128 std::move(memory), id); |
| 110 } | 129 } |
| 111 | 130 |
| 112 std::unique_ptr<cc::SharedBitmap> | 131 std::unique_ptr<cc::SharedBitmap> |
| 113 ChildSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size&, | 132 ChildSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size&, |
| 114 const cc::SharedBitmapId&) { | 133 const cc::SharedBitmapId&) { |
| 115 NOTREACHED(); | 134 NOTREACHED(); |
| 116 return nullptr; | 135 return nullptr; |
| 117 } | 136 } |
| 118 | 137 |
| 119 std::unique_ptr<cc::SharedBitmap> | 138 std::unique_ptr<cc::SharedBitmap> |
| 120 ChildSharedBitmapManager::GetBitmapForSharedMemory(base::SharedMemory* mem) { | 139 ChildSharedBitmapManager::GetBitmapForSharedMemory(base::SharedMemory* mem) { |
| 121 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | 140 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); |
| 122 NotifyAllocatedSharedBitmap(mem, cc::SharedBitmap::GenerateId()); | 141 NotifyAllocatedSharedBitmap(mem, cc::SharedBitmap::GenerateId()); |
| 123 return base::MakeUnique<ChildSharedBitmap>(render_message_filter_ptr_, | 142 return base::MakeUnique<ChildSharedBitmap>(shared_bitmap_manager_ptr_, |
| 124 std::move(mem), id); | 143 std::move(mem), id); |
| 125 } | 144 } |
| 126 | 145 |
| 127 // Notifies the browser process that a shared bitmap with the given ID was | 146 // Notifies the browser process that a shared bitmap with the given ID was |
| 128 // allocated. Caller keeps ownership of |memory|. | 147 // allocated. Caller keeps ownership of |memory|. |
| 129 void ChildSharedBitmapManager::NotifyAllocatedSharedBitmap( | 148 void ChildSharedBitmapManager::NotifyAllocatedSharedBitmap( |
| 130 base::SharedMemory* memory, | 149 base::SharedMemory* memory, |
| 131 const cc::SharedBitmapId& id) { | 150 const cc::SharedBitmapId& id) { |
| 132 base::SharedMemoryHandle handle_to_send = | 151 base::SharedMemoryHandle handle_to_send = |
| 133 base::SharedMemory::DuplicateHandle(memory->handle()); | 152 base::SharedMemory::DuplicateHandle(memory->handle()); |
| 134 if (!base::SharedMemory::IsHandleValid(handle_to_send)) { | 153 if (!base::SharedMemory::IsHandleValid(handle_to_send)) { |
| 135 LOG(ERROR) << "Failed to duplicate shared memory handle for bitmap."; | 154 LOG(ERROR) << "Failed to duplicate shared memory handle for bitmap."; |
| 136 return; | 155 return; |
| 137 } | 156 } |
| 138 | 157 |
| 139 mojo::ScopedSharedBufferHandle buffer_handle = mojo::WrapSharedMemoryHandle( | 158 mojo::ScopedSharedBufferHandle buffer_handle = mojo::WrapSharedMemoryHandle( |
| 140 handle_to_send, memory->mapped_size(), true /* read_only */); | 159 handle_to_send, memory->mapped_size(), true /* read_only */); |
| 141 | 160 |
| 142 (*render_message_filter_ptr_) | 161 (*shared_bitmap_manager_ptr_) |
| 143 ->AllocatedSharedBitmap(std::move(buffer_handle), id); | 162 ->DidAllocateSharedBitmap(std::move(buffer_handle), id); |
| 144 } | 163 } |
| 145 | 164 |
| 146 } // namespace content | 165 } // namespace ui |
| OLD | NEW |