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

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 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)
92 : render_message_filter_ptr_(render_message_filter_ptr) {
84 } 93 }
85 94
86 ChildSharedBitmapManager::~ChildSharedBitmapManager() {} 95 ChildSharedBitmapManager::~ChildSharedBitmapManager() {}
87 96
88 std::unique_ptr<cc::SharedBitmap> 97 std::unique_ptr<cc::SharedBitmap>
89 ChildSharedBitmapManager::AllocateSharedBitmap(const gfx::Size& size) { 98 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", 99 TRACE_EVENT2("renderer",
102 "ChildSharedBitmapManager::AllocateSharedMemoryBitmap", "width", 100 "ChildSharedBitmapManager::AllocateSharedMemoryBitmap", "width",
103 size.width(), "height", size.height()); 101 size.width(), "height", size.height());
104 size_t memory_size; 102 size_t memory_size;
105 if (!cc::SharedBitmap::SizeInBytes(size, &memory_size)) 103 if (!cc::SharedBitmap::SizeInBytes(size, &memory_size))
106 return std::unique_ptr<SharedMemoryBitmap>(); 104 return std::unique_ptr<SharedMemoryBitmap>();
107 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); 105 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
108 std::unique_ptr<base::SharedMemory> memory; 106 bool out_of_memory = false;
109 #if defined(OS_POSIX) 107 std::unique_ptr<base::SharedMemory> memory =
110 base::SharedMemoryHandle handle; 108 ChildThreadImpl::AllocateSharedMemory(memory_size, nullptr,
111 bool send_success = 109 &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) { 110 if (!memory) {
128 if (out_of_memory) { 111 if (out_of_memory) {
129 CollectMemoryUsageAndDie(size, memory_size); 112 CollectMemoryUsageAndDie(size, memory_size);
130 } else { 113 } else {
131 // Callers of this method are not prepared to handle failures during 114 // Callers of this method are not prepared to handle failures during
132 // shutdown. Exit immediately. This is expected behavior during the Fast 115 // shutdown. Exit immediately. This is expected behavior during the Fast
133 // Shutdown path, so use EXIT_SUCCESS. https://crbug.com/615121. 116 // Shutdown path, so use EXIT_SUCCESS. https://crbug.com/615121.
134 exit(EXIT_SUCCESS); 117 exit(EXIT_SUCCESS);
135 } 118 }
136 } 119 }
137 120
138 if (!memory->Map(memory_size)) 121 if (!memory->Map(memory_size))
139 CollectMemoryUsageAndDie(size, memory_size); 122 CollectMemoryUsageAndDie(size, memory_size);
140 123
141 base::SharedMemoryHandle handle_to_send = memory->handle(); 124 NotifyAllocatedSharedBitmap(memory.get(), id);
142 sender_->Send(new ChildProcessHostMsg_AllocatedSharedBitmap( 125
143 memory_size, handle_to_send, id)); 126 return base::MakeUnique<ChildSharedBitmap>(render_message_filter_ptr_,
144 #endif 127 std::move(memory), id);
145 return base::MakeUnique<ChildSharedBitmap>(sender_, std::move(memory), id);
146 } 128 }
147 129
148 std::unique_ptr<cc::SharedBitmap> 130 std::unique_ptr<cc::SharedBitmap>
149 ChildSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size&, 131 ChildSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size&,
150 const cc::SharedBitmapId&) { 132 const cc::SharedBitmapId&) {
151 NOTREACHED(); 133 NOTREACHED();
152 return std::unique_ptr<cc::SharedBitmap>(); 134 return std::unique_ptr<cc::SharedBitmap>();
153 } 135 }
154 136
155 std::unique_ptr<cc::SharedBitmap> 137 std::unique_ptr<cc::SharedBitmap>
156 ChildSharedBitmapManager::GetBitmapForSharedMemory(base::SharedMemory* mem) { 138 ChildSharedBitmapManager::GetBitmapForSharedMemory(base::SharedMemory* mem) {
157 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); 139 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
158 base::SharedMemoryHandle handle_to_send = mem->handle(); 140 NotifyAllocatedSharedBitmap(mem, cc::SharedBitmap::GenerateId());
141 return base::MakeUnique<ChildSharedBitmap>(render_message_filter_ptr_,
142 std::move(mem), id);
143 }
144
145 // Notifies the browser process that a shared bitmap with the given ID was
146 // allocated. Caller keeps ownership of |memory|.
147 void ChildSharedBitmapManager::NotifyAllocatedSharedBitmap(
148 base::SharedMemory* memory,
149 const cc::SharedBitmapId& id) {
150 base::SharedMemoryHandle handle_to_send = memory->handle();
Ken Rockot(use gerrit already) 2016/11/29 17:43:11 This seems to correctly mirror the old behavior, b
Jay Civelli 2016/11/29 18:01:48 Good point, done.
159 #if defined(OS_POSIX) 151 #if defined(OS_POSIX)
160 if (!mem->ShareToProcess(base::GetCurrentProcessHandle(), &handle_to_send)) 152 if (!memory->ShareToProcess(base::GetCurrentProcessHandle(), &handle_to_send))
161 return std::unique_ptr<cc::SharedBitmap>(); 153 return;
162 #endif 154 #endif
163 sender_->Send(new ChildProcessHostMsg_AllocatedSharedBitmap(
164 mem->mapped_size(), handle_to_send, id));
165 155
166 return base::MakeUnique<ChildSharedBitmap>(sender_, mem, id); 156 mojo::ScopedSharedBufferHandle buffer_handle =
157 mojo::WrapSharedMemoryHandle(
158 base::SharedMemory::DuplicateHandle(handle_to_send),
159 memory->mapped_size(),
160 true /* read_only */);
161
162 (*render_message_filter_ptr_)->
163 AllocatedSharedBitmap(std::move(buffer_handle), id);
167 } 164 }
168 165
169 } // namespace content 166 } // 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