Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(379)

Side by Side Diff: content/child/child_shared_bitmap_manager.cc

Issue 2488913003: Replacing allocate bitmap IPC messages with a new Mojo interface. (Closed)
Patch Set: Clean-up Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/child/child_shared_bitmap_manager.h ('k') | content/child/child_thread_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
18 #include "ui/gfx/geometry/size.h" 19 #include "ui/gfx/geometry/size.h"
19 20
20 namespace content { 21 namespace content {
21 22
22 namespace { 23 namespace {
23 24
24 class ChildSharedBitmap : public SharedMemoryBitmap { 25 class ChildSharedBitmap : public SharedMemoryBitmap {
25 public: 26 public:
26 ChildSharedBitmap(scoped_refptr<ThreadSafeSender> sender, 27 ChildSharedBitmap(const base::WeakPtr<ChildSharedBitmapManager>& manager,
28 scoped_refptr<ThreadSafeSender> sender,
27 base::SharedMemory* shared_memory, 29 base::SharedMemory* shared_memory,
28 const cc::SharedBitmapId& id) 30 const cc::SharedBitmapId& id)
29 : SharedMemoryBitmap(static_cast<uint8_t*>(shared_memory->memory()), 31 : SharedMemoryBitmap(static_cast<uint8_t*>(shared_memory->memory()),
30 id, 32 id,
31 shared_memory), 33 shared_memory),
32 sender_(sender) {} 34 manager_(manager) {}
33 35
34 ChildSharedBitmap(scoped_refptr<ThreadSafeSender> sender, 36 ChildSharedBitmap(const base::WeakPtr<ChildSharedBitmapManager>& manager,
37 scoped_refptr<ThreadSafeSender> sender,
35 std::unique_ptr<base::SharedMemory> shared_memory_holder, 38 std::unique_ptr<base::SharedMemory> shared_memory_holder,
36 const cc::SharedBitmapId& id) 39 const cc::SharedBitmapId& id)
37 : ChildSharedBitmap(sender, shared_memory_holder.get(), id) { 40 : ChildSharedBitmap(manager, sender, shared_memory_holder.get(), id) {
38 shared_memory_holder_ = std::move(shared_memory_holder); 41 shared_memory_holder_ = std::move(shared_memory_holder);
39 } 42 }
40 43
41 ~ChildSharedBitmap() override { 44 ~ChildSharedBitmap() override {
42 sender_->Send(new ChildProcessHostMsg_DeletedSharedBitmap(id())); 45 if (manager_)
46 manager_->OnBitmapDeleted(id());
43 } 47 }
44 48
45 private: 49 private:
46 scoped_refptr<ThreadSafeSender> sender_; 50 base::WeakPtr<ChildSharedBitmapManager> manager_;
47 std::unique_ptr<base::SharedMemory> shared_memory_holder_; 51 std::unique_ptr<base::SharedMemory> shared_memory_holder_;
48 }; 52 };
49 53
50 // Collect extra information for debugging bitmap creation failures. 54 // Collect extra information for debugging bitmap creation failures.
51 void CollectMemoryUsageAndDie(const gfx::Size& size, size_t alloc_size) { 55 void CollectMemoryUsageAndDie(const gfx::Size& size, size_t alloc_size) {
52 #if defined(OS_WIN) 56 #if defined(OS_WIN)
53 int width = size.width(); 57 int width = size.width();
54 int height = size.height(); 58 int height = size.height();
55 DWORD last_error = GetLastError(); 59 DWORD last_error = GetLastError();
56 60
(...skipping 14 matching lines...) Expand all
71 base::TerminateBecauseOutOfMemory(alloc_size); 75 base::TerminateBecauseOutOfMemory(alloc_size);
72 } 76 }
73 77
74 } // namespace 78 } // namespace
75 79
76 SharedMemoryBitmap::SharedMemoryBitmap(uint8_t* pixels, 80 SharedMemoryBitmap::SharedMemoryBitmap(uint8_t* pixels,
77 const cc::SharedBitmapId& id, 81 const cc::SharedBitmapId& id,
78 base::SharedMemory* shared_memory) 82 base::SharedMemory* shared_memory)
79 : SharedBitmap(pixels, id), shared_memory_(shared_memory) {} 83 : SharedBitmap(pixels, id), shared_memory_(shared_memory) {}
80 84
85 // Store the interface pointer info, as the actual methods are called from a
86 // different thread (the compositor thread). We'll bind the ptr on the first
87 // call.
81 ChildSharedBitmapManager::ChildSharedBitmapManager( 88 ChildSharedBitmapManager::ChildSharedBitmapManager(
82 scoped_refptr<ThreadSafeSender> sender) 89 scoped_refptr<ThreadSafeSender> sender,
83 : sender_(sender) { 90 mojom::SharedMemoryAllocatorPtr shared_memory_allocator)
91 : sender_(sender),
92 shared_memory_allocator_ptr_info_(
93 shared_memory_allocator.PassInterface()),
94 weak_factory_(this) {
84 } 95 }
85 96
86 ChildSharedBitmapManager::~ChildSharedBitmapManager() {} 97 ChildSharedBitmapManager::~ChildSharedBitmapManager() {}
87 98
88 std::unique_ptr<cc::SharedBitmap> 99 std::unique_ptr<cc::SharedBitmap>
89 ChildSharedBitmapManager::AllocateSharedBitmap(const gfx::Size& size) { 100 ChildSharedBitmapManager::AllocateSharedBitmap(const gfx::Size& size) {
90 std::unique_ptr<SharedMemoryBitmap> bitmap = AllocateSharedMemoryBitmap(size); 101 std::unique_ptr<SharedMemoryBitmap> bitmap = AllocateSharedMemoryBitmap(size);
91 #if defined(OS_POSIX) 102 #if defined(OS_POSIX)
92 // Close file descriptor to avoid running out. 103 // Close file descriptor to avoid running out.
93 if (bitmap) 104 if (bitmap)
94 bitmap->shared_memory()->Close(); 105 bitmap->shared_memory()->Close();
95 #endif 106 #endif
96 return std::move(bitmap); 107 return std::move(bitmap);
97 } 108 }
98 109
99 std::unique_ptr<SharedMemoryBitmap> 110 std::unique_ptr<SharedMemoryBitmap>
100 ChildSharedBitmapManager::AllocateSharedMemoryBitmap(const gfx::Size& size) { 111 ChildSharedBitmapManager::AllocateSharedMemoryBitmap(const gfx::Size& size) {
101 TRACE_EVENT2("renderer", 112 TRACE_EVENT2("renderer",
102 "ChildSharedBitmapManager::AllocateSharedMemoryBitmap", "width", 113 "ChildSharedBitmapManager::AllocateSharedMemoryBitmap", "width",
103 size.width(), "height", size.height()); 114 size.width(), "height", size.height());
104 size_t memory_size; 115 size_t memory_size;
105 if (!cc::SharedBitmap::SizeInBytes(size, &memory_size)) 116 if (!cc::SharedBitmap::SizeInBytes(size, &memory_size))
106 return std::unique_ptr<SharedMemoryBitmap>(); 117 return std::unique_ptr<SharedMemoryBitmap>();
107 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); 118 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
108 std::unique_ptr<base::SharedMemory> memory; 119 bool out_of_memory = false;
109 #if defined(OS_POSIX) 120 std::unique_ptr<base::SharedMemory> memory =
110 base::SharedMemoryHandle handle; 121 ChildThreadImpl::AllocateSharedMemory(memory_size, sender_.get(),
111 bool send_success = 122 &out_of_memory);
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);
127 if (!memory) { 123 if (!memory) {
128 if (out_of_memory) { 124 if (out_of_memory) {
129 CollectMemoryUsageAndDie(size, memory_size); 125 CollectMemoryUsageAndDie(size, memory_size);
130 } else { 126 } else {
131 // Callers of this method are not prepared to handle failures during 127 // Callers of this method are not prepared to handle failures during
132 // shutdown. Exit immediately. This is expected behavior during the Fast 128 // shutdown. Exit immediately. This is expected behavior during the Fast
133 // Shutdown path, so use EXIT_SUCCESS. https://crbug.com/615121. 129 // Shutdown path, so use EXIT_SUCCESS. https://crbug.com/615121.
134 exit(EXIT_SUCCESS); 130 exit(EXIT_SUCCESS);
135 } 131 }
136 } 132 }
137 133
138 if (!memory->Map(memory_size)) 134 if (!memory->Map(memory_size))
139 CollectMemoryUsageAndDie(size, memory_size); 135 CollectMemoryUsageAndDie(size, memory_size);
140 136
141 base::SharedMemoryHandle handle_to_send = memory->handle(); 137 NotifyAllocatedSharedBitmap(memory.get(), id);
142 sender_->Send(new ChildProcessHostMsg_AllocatedSharedBitmap( 138
143 memory_size, handle_to_send, id)); 139 return base::MakeUnique<ChildSharedBitmap>(weak_factory_.GetWeakPtr(),
144 #endif 140 sender_, std::move(memory), id);
145 return base::MakeUnique<ChildSharedBitmap>(sender_, std::move(memory), id);
146 } 141 }
147 142
148 std::unique_ptr<cc::SharedBitmap> 143 std::unique_ptr<cc::SharedBitmap>
149 ChildSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size&, 144 ChildSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size&,
150 const cc::SharedBitmapId&) { 145 const cc::SharedBitmapId&) {
151 NOTREACHED(); 146 NOTREACHED();
152 return std::unique_ptr<cc::SharedBitmap>(); 147 return std::unique_ptr<cc::SharedBitmap>();
153 } 148 }
154 149
155 std::unique_ptr<cc::SharedBitmap> 150 std::unique_ptr<cc::SharedBitmap>
156 ChildSharedBitmapManager::GetBitmapForSharedMemory(base::SharedMemory* mem) { 151 ChildSharedBitmapManager::GetBitmapForSharedMemory(base::SharedMemory* mem) {
157 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); 152 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
158 base::SharedMemoryHandle handle_to_send = mem->handle(); 153 NotifyAllocatedSharedBitmap(mem, cc::SharedBitmap::GenerateId());
154 return base::MakeUnique<ChildSharedBitmap>(weak_factory_.GetWeakPtr(),
155 sender_, std::move(mem), id);
156 }
157
158 void ChildSharedBitmapManager::OnBitmapDeleted(const cc::SharedBitmapId& id) {
159 shared_memory_allocator_->DeletedSharedBitmap(id);
160 }
161
162 void ChildSharedBitmapManager::FinishInitialization() {
163 if (shared_memory_allocator_.is_bound())
164 return; // Already initialized.
165
166 shared_memory_allocator_.Bind(std::move(shared_memory_allocator_ptr_info_));
167 }
168
169 // Notifies the browser process that a shared bitmap with the given ID was
170 // allocated. Caller keeps ownership of |memory|.
171 void ChildSharedBitmapManager::NotifyAllocatedSharedBitmap(
172 base::SharedMemory* memory,
173 const cc::SharedBitmapId& id) {
174 // If we have not bound our interface, do it now that we are on the correct
175 // thread.
176 FinishInitialization();
177
178 base::SharedMemoryHandle handle_to_send = memory->handle();
159 #if defined(OS_POSIX) 179 #if defined(OS_POSIX)
160 if (!mem->ShareToProcess(base::GetCurrentProcessHandle(), &handle_to_send)) 180 if (!memory->ShareToProcess(base::GetCurrentProcessHandle(), &handle_to_send))
161 return std::unique_ptr<cc::SharedBitmap>(); 181 return;
162 #endif 182 #endif
163 sender_->Send(new ChildProcessHostMsg_AllocatedSharedBitmap(
164 mem->mapped_size(), handle_to_send, id));
165 183
166 return base::MakeUnique<ChildSharedBitmap>(sender_, mem, id); 184 mojo::ScopedSharedBufferHandle buffer_handle =
185 mojo::WrapSharedMemoryHandle(
186 base::SharedMemory::DuplicateHandle(handle_to_send),
187 memory->mapped_size(),
188 true /* read_only */);
Jay Civelli 2016/11/09 21:47:11 @jbauman Existing code does not share the memory a
189
190 shared_memory_allocator_->AllocatedSharedBitmap(std::move(buffer_handle), id);
167 } 191 }
168 192
169 } // namespace content 193 } // namespace content
OLDNEW
« no previous file with comments | « content/child/child_shared_bitmap_manager.h ('k') | content/child/child_thread_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698