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

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

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

Powered by Google App Engine
This is Rietveld 408576698