| 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 "content/child/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 "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "content/child/child_thread_impl.h" | 16 #include "content/child/child_thread_impl.h" |
| 17 #include "content/common/child_process_messages.h" | 17 #include "content/common/child_process_messages.h" |
| 18 #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 content { |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 class ChildSharedBitmap : public SharedMemoryBitmap { | 24 class ChildSharedBitmap : public SharedMemoryBitmap { |
| 26 public: | 25 public: |
| 27 ChildSharedBitmap( | 26 ChildSharedBitmap(scoped_refptr<ThreadSafeSender> sender, |
| 28 const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>& | 27 base::SharedMemory* shared_memory, |
| 29 render_message_filter_ptr, | 28 const cc::SharedBitmapId& id) |
| 30 base::SharedMemory* shared_memory, | |
| 31 const cc::SharedBitmapId& id) | |
| 32 : SharedMemoryBitmap(static_cast<uint8_t*>(shared_memory->memory()), | 29 : SharedMemoryBitmap(static_cast<uint8_t*>(shared_memory->memory()), |
| 33 id, | 30 id, |
| 34 shared_memory), | 31 shared_memory), |
| 35 render_message_filter_ptr_(render_message_filter_ptr) {} | 32 sender_(sender) {} |
| 36 | 33 |
| 37 ChildSharedBitmap( | 34 ChildSharedBitmap(scoped_refptr<ThreadSafeSender> sender, |
| 38 const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>& | 35 std::unique_ptr<base::SharedMemory> shared_memory_holder, |
| 39 render_message_filter_ptr, | 36 const cc::SharedBitmapId& id) |
| 40 std::unique_ptr<base::SharedMemory> shared_memory_holder, | 37 : ChildSharedBitmap(sender, shared_memory_holder.get(), id) { |
| 41 const cc::SharedBitmapId& id) | |
| 42 : ChildSharedBitmap(render_message_filter_ptr, | |
| 43 shared_memory_holder.get(), | |
| 44 id) { | |
| 45 shared_memory_holder_ = std::move(shared_memory_holder); | 38 shared_memory_holder_ = std::move(shared_memory_holder); |
| 46 } | 39 } |
| 47 | 40 |
| 48 ~ChildSharedBitmap() override { | 41 ~ChildSharedBitmap() override { |
| 49 (*render_message_filter_ptr_)->DeletedSharedBitmap(id()); | 42 sender_->Send(new ChildProcessHostMsg_DeletedSharedBitmap(id())); |
| 50 } | 43 } |
| 51 | 44 |
| 52 private: | 45 private: |
| 53 scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr> | 46 scoped_refptr<ThreadSafeSender> sender_; |
| 54 render_message_filter_ptr_; | |
| 55 std::unique_ptr<base::SharedMemory> shared_memory_holder_; | 47 std::unique_ptr<base::SharedMemory> shared_memory_holder_; |
| 56 }; | 48 }; |
| 57 | 49 |
| 58 // Collect extra information for debugging bitmap creation failures. | 50 // Collect extra information for debugging bitmap creation failures. |
| 59 void CollectMemoryUsageAndDie(const gfx::Size& size, size_t alloc_size) { | 51 void CollectMemoryUsageAndDie(const gfx::Size& size, size_t alloc_size) { |
| 60 #if defined(OS_WIN) | 52 #if defined(OS_WIN) |
| 61 int width = size.width(); | 53 int width = size.width(); |
| 62 int height = size.height(); | 54 int height = size.height(); |
| 63 DWORD last_error = GetLastError(); | 55 DWORD last_error = GetLastError(); |
| 64 | 56 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 80 } | 72 } |
| 81 | 73 |
| 82 } // namespace | 74 } // namespace |
| 83 | 75 |
| 84 SharedMemoryBitmap::SharedMemoryBitmap(uint8_t* pixels, | 76 SharedMemoryBitmap::SharedMemoryBitmap(uint8_t* pixels, |
| 85 const cc::SharedBitmapId& id, | 77 const cc::SharedBitmapId& id, |
| 86 base::SharedMemory* shared_memory) | 78 base::SharedMemory* shared_memory) |
| 87 : SharedBitmap(pixels, id), shared_memory_(shared_memory) {} | 79 : SharedBitmap(pixels, id), shared_memory_(shared_memory) {} |
| 88 | 80 |
| 89 ChildSharedBitmapManager::ChildSharedBitmapManager( | 81 ChildSharedBitmapManager::ChildSharedBitmapManager( |
| 90 const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>& | 82 scoped_refptr<ThreadSafeSender> sender) |
| 91 render_message_filter_ptr) | 83 : sender_(sender) { |
| 92 : render_message_filter_ptr_(render_message_filter_ptr) {} | 84 } |
| 93 | 85 |
| 94 ChildSharedBitmapManager::~ChildSharedBitmapManager() {} | 86 ChildSharedBitmapManager::~ChildSharedBitmapManager() {} |
| 95 | 87 |
| 96 std::unique_ptr<cc::SharedBitmap> | 88 std::unique_ptr<cc::SharedBitmap> |
| 97 ChildSharedBitmapManager::AllocateSharedBitmap(const gfx::Size& size) { | 89 ChildSharedBitmapManager::AllocateSharedBitmap(const gfx::Size& size) { |
| 90 std::unique_ptr<SharedMemoryBitmap> bitmap = AllocateSharedMemoryBitmap(size); |
| 91 #if defined(OS_POSIX) |
| 92 // Close file descriptor to avoid running out. |
| 93 if (bitmap) |
| 94 bitmap->shared_memory()->Close(); |
| 95 #endif |
| 96 return std::move(bitmap); |
| 97 } |
| 98 |
| 99 std::unique_ptr<SharedMemoryBitmap> |
| 100 ChildSharedBitmapManager::AllocateSharedMemoryBitmap(const gfx::Size& size) { |
| 98 TRACE_EVENT2("renderer", | 101 TRACE_EVENT2("renderer", |
| 99 "ChildSharedBitmapManager::AllocateSharedMemoryBitmap", "width", | 102 "ChildSharedBitmapManager::AllocateSharedMemoryBitmap", "width", |
| 100 size.width(), "height", size.height()); | 103 size.width(), "height", size.height()); |
| 101 size_t memory_size; | 104 size_t memory_size; |
| 102 if (!cc::SharedBitmap::SizeInBytes(size, &memory_size)) | 105 if (!cc::SharedBitmap::SizeInBytes(size, &memory_size)) |
| 103 return std::unique_ptr<SharedMemoryBitmap>(); | 106 return std::unique_ptr<SharedMemoryBitmap>(); |
| 104 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | 107 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); |
| 105 bool out_of_memory = false; | 108 std::unique_ptr<base::SharedMemory> memory; |
| 106 std::unique_ptr<base::SharedMemory> memory = | 109 #if defined(OS_POSIX) |
| 107 ChildThreadImpl::AllocateSharedMemory(memory_size, nullptr, | 110 base::SharedMemoryHandle handle; |
| 108 &out_of_memory); | 111 bool send_success = |
| 112 sender_->Send(new ChildProcessHostMsg_SyncAllocateSharedBitmap( |
| 113 memory_size, id, &handle)); |
| 114 if (!send_success) { |
| 115 // Callers of this method are not prepared to handle failures during |
| 116 // shutdown. Exit immediately. This is expected behavior during the Fast |
| 117 // Shutdown path, so use EXIT_SUCCESS. https://crbug.com/615121. |
| 118 exit(EXIT_SUCCESS); |
| 119 } |
| 120 memory = base::MakeUnique<base::SharedMemory>(handle, false); |
| 121 if (!memory->Map(memory_size)) |
| 122 CollectMemoryUsageAndDie(size, memory_size); |
| 123 #else |
| 124 bool out_of_memory; |
| 125 memory = ChildThreadImpl::AllocateSharedMemory(memory_size, sender_.get(), |
| 126 &out_of_memory); |
| 109 if (!memory) { | 127 if (!memory) { |
| 110 if (out_of_memory) { | 128 if (out_of_memory) { |
| 111 CollectMemoryUsageAndDie(size, memory_size); | 129 CollectMemoryUsageAndDie(size, memory_size); |
| 112 } else { | 130 } else { |
| 113 // Callers of this method are not prepared to handle failures during | 131 // Callers of this method are not prepared to handle failures during |
| 114 // shutdown. Exit immediately. This is expected behavior during the Fast | 132 // shutdown. Exit immediately. This is expected behavior during the Fast |
| 115 // Shutdown path, so use EXIT_SUCCESS. https://crbug.com/615121. | 133 // Shutdown path, so use EXIT_SUCCESS. https://crbug.com/615121. |
| 116 exit(EXIT_SUCCESS); | 134 exit(EXIT_SUCCESS); |
| 117 } | 135 } |
| 118 } | 136 } |
| 119 | 137 |
| 120 if (!memory->Map(memory_size)) | 138 if (!memory->Map(memory_size)) |
| 121 CollectMemoryUsageAndDie(size, memory_size); | 139 CollectMemoryUsageAndDie(size, memory_size); |
| 122 | 140 |
| 123 NotifyAllocatedSharedBitmap(memory.get(), id); | 141 base::SharedMemoryHandle handle_to_send = memory->handle(); |
| 124 | 142 sender_->Send(new ChildProcessHostMsg_AllocatedSharedBitmap( |
| 125 // Close the associated FD to save resources, the previously mapped memory | 143 memory_size, handle_to_send, id)); |
| 126 // remains available. | 144 #endif |
| 127 memory->Close(); | 145 return base::MakeUnique<ChildSharedBitmap>(sender_, std::move(memory), id); |
| 128 | |
| 129 return base::MakeUnique<ChildSharedBitmap>(render_message_filter_ptr_, | |
| 130 std::move(memory), id); | |
| 131 } | 146 } |
| 132 | 147 |
| 133 std::unique_ptr<cc::SharedBitmap> | 148 std::unique_ptr<cc::SharedBitmap> |
| 134 ChildSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size&, | 149 ChildSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size&, |
| 135 const cc::SharedBitmapId&) { | 150 const cc::SharedBitmapId&) { |
| 136 NOTREACHED(); | 151 NOTREACHED(); |
| 137 return std::unique_ptr<cc::SharedBitmap>(); | 152 return std::unique_ptr<cc::SharedBitmap>(); |
| 138 } | 153 } |
| 139 | 154 |
| 140 std::unique_ptr<cc::SharedBitmap> | 155 std::unique_ptr<cc::SharedBitmap> |
| 141 ChildSharedBitmapManager::GetBitmapForSharedMemory(base::SharedMemory* mem) { | 156 ChildSharedBitmapManager::GetBitmapForSharedMemory(base::SharedMemory* mem) { |
| 142 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | 157 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); |
| 143 NotifyAllocatedSharedBitmap(mem, cc::SharedBitmap::GenerateId()); | 158 base::SharedMemoryHandle handle_to_send = mem->handle(); |
| 144 return base::MakeUnique<ChildSharedBitmap>(render_message_filter_ptr_, | 159 #if defined(OS_POSIX) |
| 145 std::move(mem), id); | 160 if (!mem->ShareToProcess(base::GetCurrentProcessHandle(), &handle_to_send)) |
| 146 } | 161 return std::unique_ptr<cc::SharedBitmap>(); |
| 162 #endif |
| 163 sender_->Send(new ChildProcessHostMsg_AllocatedSharedBitmap( |
| 164 mem->mapped_size(), handle_to_send, id)); |
| 147 | 165 |
| 148 // Notifies the browser process that a shared bitmap with the given ID was | 166 return base::MakeUnique<ChildSharedBitmap>(sender_, mem, id); |
| 149 // allocated. Caller keeps ownership of |memory|. | |
| 150 void ChildSharedBitmapManager::NotifyAllocatedSharedBitmap( | |
| 151 base::SharedMemory* memory, | |
| 152 const cc::SharedBitmapId& id) { | |
| 153 base::SharedMemoryHandle handle_to_send = | |
| 154 base::SharedMemory::DuplicateHandle(memory->handle()); | |
| 155 if (!base::SharedMemory::IsHandleValid(handle_to_send)) { | |
| 156 LOG(ERROR) << "Failed to duplicate shared memory handle for bitmap."; | |
| 157 return; | |
| 158 } | |
| 159 | |
| 160 mojo::ScopedSharedBufferHandle buffer_handle = mojo::WrapSharedMemoryHandle( | |
| 161 handle_to_send, memory->mapped_size(), true /* read_only */); | |
| 162 | |
| 163 (*render_message_filter_ptr_) | |
| 164 ->AllocatedSharedBitmap(std::move(buffer_handle), id); | |
| 165 } | 167 } |
| 166 | 168 |
| 167 } // namespace content | 169 } // namespace content |
| OLD | NEW |