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 "components/display_compositor/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 "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 display_compositor { |
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( |
28 const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>& | 27 const scoped_refptr<mojom::ThreadSafeSharedBitmapManagerAssociatedPtr>& |
29 render_message_filter_ptr, | 28 render_message_filter_ptr, |
30 base::SharedMemory* shared_memory, | 29 base::SharedMemory* shared_memory, |
31 const cc::SharedBitmapId& id) | 30 const cc::SharedBitmapId& id) |
32 : SharedMemoryBitmap(static_cast<uint8_t*>(shared_memory->memory()), | 31 : SharedMemoryBitmap(static_cast<uint8_t*>(shared_memory->memory()), |
33 id, | 32 id, |
34 shared_memory), | 33 shared_memory), |
35 render_message_filter_ptr_(render_message_filter_ptr) {} | 34 shared_bitmap_manager_ptr_(render_message_filter_ptr) {} |
36 | 35 |
37 ChildSharedBitmap( | 36 ChildSharedBitmap( |
38 const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>& | 37 const scoped_refptr<mojom::ThreadSafeSharedBitmapManagerAssociatedPtr>& |
39 render_message_filter_ptr, | 38 render_message_filter_ptr, |
40 std::unique_ptr<base::SharedMemory> shared_memory_holder, | 39 std::unique_ptr<base::SharedMemory> shared_memory_holder, |
41 const cc::SharedBitmapId& id) | 40 const cc::SharedBitmapId& id) |
42 : ChildSharedBitmap(render_message_filter_ptr, | 41 : ChildSharedBitmap(render_message_filter_ptr, |
43 shared_memory_holder.get(), | 42 shared_memory_holder.get(), |
44 id) { | 43 id) { |
45 shared_memory_holder_ = std::move(shared_memory_holder); | 44 shared_memory_holder_ = std::move(shared_memory_holder); |
46 } | 45 } |
47 | 46 |
48 ~ChildSharedBitmap() override { | 47 ~ChildSharedBitmap() override { |
49 (*render_message_filter_ptr_)->DeletedSharedBitmap(id()); | 48 (*shared_bitmap_manager_ptr_)->DeletedSharedBitmap(id()); |
50 } | 49 } |
51 | 50 |
52 private: | 51 private: |
53 scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr> | 52 scoped_refptr<mojom::ThreadSafeSharedBitmapManagerAssociatedPtr> |
54 render_message_filter_ptr_; | 53 shared_bitmap_manager_ptr_; |
55 std::unique_ptr<base::SharedMemory> shared_memory_holder_; | 54 std::unique_ptr<base::SharedMemory> shared_memory_holder_; |
56 }; | 55 }; |
57 | 56 |
58 // Collect extra information for debugging bitmap creation failures. | 57 // Collect extra information for debugging bitmap creation failures. |
59 void CollectMemoryUsageAndDie(const gfx::Size& size, size_t alloc_size) { | 58 void CollectMemoryUsageAndDie(const gfx::Size& size, size_t alloc_size) { |
60 #if defined(OS_WIN) | 59 #if defined(OS_WIN) |
61 int width = size.width(); | 60 int width = size.width(); |
62 int height = size.height(); | 61 int height = size.height(); |
63 DWORD last_error = GetLastError(); | 62 DWORD last_error = GetLastError(); |
64 | 63 |
(...skipping 15 matching lines...) Expand all Loading... | |
80 } | 79 } |
81 | 80 |
82 } // namespace | 81 } // namespace |
83 | 82 |
84 SharedMemoryBitmap::SharedMemoryBitmap(uint8_t* pixels, | 83 SharedMemoryBitmap::SharedMemoryBitmap(uint8_t* pixels, |
85 const cc::SharedBitmapId& id, | 84 const cc::SharedBitmapId& id, |
86 base::SharedMemory* shared_memory) | 85 base::SharedMemory* shared_memory) |
87 : SharedBitmap(pixels, id), shared_memory_(shared_memory) {} | 86 : SharedBitmap(pixels, id), shared_memory_(shared_memory) {} |
88 | 87 |
89 ChildSharedBitmapManager::ChildSharedBitmapManager( | 88 ChildSharedBitmapManager::ChildSharedBitmapManager( |
90 const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>& | 89 const scoped_refptr<mojom::ThreadSafeSharedBitmapManagerAssociatedPtr>& |
91 render_message_filter_ptr) | 90 render_message_filter_ptr) |
92 : render_message_filter_ptr_(render_message_filter_ptr) {} | 91 : shared_bitmap_manager_ptr_(render_message_filter_ptr) {} |
93 | 92 |
94 ChildSharedBitmapManager::~ChildSharedBitmapManager() {} | 93 ChildSharedBitmapManager::~ChildSharedBitmapManager() {} |
95 | 94 |
95 // static | |
96 std::unique_ptr<base::SharedMemory> | |
97 ChildSharedBitmapManager::AllocateSharedMemory(size_t buf_size) { | |
danakj
2017/02/28 20:34:56
This should be a private method then, in the anon
| |
98 mojo::ScopedSharedBufferHandle mojo_buf = | |
99 mojo::SharedBufferHandle::Create(buf_size); | |
100 if (!mojo_buf->is_valid()) { | |
101 LOG(WARNING) << "Browser failed to allocate shared memory"; | |
102 return nullptr; | |
103 } | |
104 | |
105 base::SharedMemoryHandle shared_buf; | |
106 if (mojo::UnwrapSharedMemoryHandle(std::move(mojo_buf), &shared_buf, nullptr, | |
107 nullptr) != MOJO_RESULT_OK) { | |
108 LOG(WARNING) << "Browser failed to allocate shared memory"; | |
109 return nullptr; | |
110 } | |
111 | |
112 return base::MakeUnique<base::SharedMemory>(shared_buf, false); | |
113 } | |
114 | |
96 std::unique_ptr<cc::SharedBitmap> | 115 std::unique_ptr<cc::SharedBitmap> |
97 ChildSharedBitmapManager::AllocateSharedBitmap(const gfx::Size& size) { | 116 ChildSharedBitmapManager::AllocateSharedBitmap(const gfx::Size& size) { |
98 TRACE_EVENT2("renderer", | 117 TRACE_EVENT2("renderer", |
99 "ChildSharedBitmapManager::AllocateSharedMemoryBitmap", "width", | 118 "ChildSharedBitmapManager::AllocateSharedMemoryBitmap", "width", |
100 size.width(), "height", size.height()); | 119 size.width(), "height", size.height()); |
101 size_t memory_size; | 120 size_t memory_size; |
102 if (!cc::SharedBitmap::SizeInBytes(size, &memory_size)) | 121 if (!cc::SharedBitmap::SizeInBytes(size, &memory_size)) |
103 return std::unique_ptr<SharedMemoryBitmap>(); | 122 return std::unique_ptr<SharedMemoryBitmap>(); |
104 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | 123 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); |
105 std::unique_ptr<base::SharedMemory> memory = | 124 std::unique_ptr<base::SharedMemory> memory = |
106 ChildThreadImpl::AllocateSharedMemory(memory_size); | 125 ChildSharedBitmapManager::AllocateSharedMemory(memory_size); |
107 if (!memory || !memory->Map(memory_size)) | 126 if (!memory || !memory->Map(memory_size)) |
108 CollectMemoryUsageAndDie(size, memory_size); | 127 CollectMemoryUsageAndDie(size, memory_size); |
109 | 128 |
110 NotifyAllocatedSharedBitmap(memory.get(), id); | 129 NotifyAllocatedSharedBitmap(memory.get(), id); |
111 | 130 |
112 // Close the associated FD to save resources, the previously mapped memory | 131 // Close the associated FD to save resources, the previously mapped memory |
113 // remains available. | 132 // remains available. |
114 memory->Close(); | 133 memory->Close(); |
115 | 134 |
116 return base::MakeUnique<ChildSharedBitmap>(render_message_filter_ptr_, | 135 return base::MakeUnique<ChildSharedBitmap>(shared_bitmap_manager_ptr_, |
117 std::move(memory), id); | 136 std::move(memory), id); |
118 } | 137 } |
119 | 138 |
120 std::unique_ptr<cc::SharedBitmap> | 139 std::unique_ptr<cc::SharedBitmap> |
121 ChildSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size&, | 140 ChildSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size&, |
122 const cc::SharedBitmapId&) { | 141 const cc::SharedBitmapId&) { |
123 NOTREACHED(); | 142 NOTREACHED(); |
124 return std::unique_ptr<cc::SharedBitmap>(); | 143 return std::unique_ptr<cc::SharedBitmap>(); |
125 } | 144 } |
126 | 145 |
127 std::unique_ptr<cc::SharedBitmap> | 146 std::unique_ptr<cc::SharedBitmap> |
128 ChildSharedBitmapManager::GetBitmapForSharedMemory(base::SharedMemory* mem) { | 147 ChildSharedBitmapManager::GetBitmapForSharedMemory(base::SharedMemory* mem) { |
129 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | 148 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); |
130 NotifyAllocatedSharedBitmap(mem, cc::SharedBitmap::GenerateId()); | 149 NotifyAllocatedSharedBitmap(mem, cc::SharedBitmap::GenerateId()); |
131 return base::MakeUnique<ChildSharedBitmap>(render_message_filter_ptr_, | 150 return base::MakeUnique<ChildSharedBitmap>(shared_bitmap_manager_ptr_, |
132 std::move(mem), id); | 151 std::move(mem), id); |
133 } | 152 } |
134 | 153 |
135 // Notifies the browser process that a shared bitmap with the given ID was | 154 // Notifies the browser process that a shared bitmap with the given ID was |
136 // allocated. Caller keeps ownership of |memory|. | 155 // allocated. Caller keeps ownership of |memory|. |
137 void ChildSharedBitmapManager::NotifyAllocatedSharedBitmap( | 156 void ChildSharedBitmapManager::NotifyAllocatedSharedBitmap( |
138 base::SharedMemory* memory, | 157 base::SharedMemory* memory, |
139 const cc::SharedBitmapId& id) { | 158 const cc::SharedBitmapId& id) { |
140 base::SharedMemoryHandle handle_to_send = | 159 base::SharedMemoryHandle handle_to_send = |
141 base::SharedMemory::DuplicateHandle(memory->handle()); | 160 base::SharedMemory::DuplicateHandle(memory->handle()); |
142 if (!base::SharedMemory::IsHandleValid(handle_to_send)) { | 161 if (!base::SharedMemory::IsHandleValid(handle_to_send)) { |
143 LOG(ERROR) << "Failed to duplicate shared memory handle for bitmap."; | 162 LOG(ERROR) << "Failed to duplicate shared memory handle for bitmap."; |
144 return; | 163 return; |
145 } | 164 } |
146 | 165 |
147 mojo::ScopedSharedBufferHandle buffer_handle = mojo::WrapSharedMemoryHandle( | 166 mojo::ScopedSharedBufferHandle buffer_handle = mojo::WrapSharedMemoryHandle( |
148 handle_to_send, memory->mapped_size(), true /* read_only */); | 167 handle_to_send, memory->mapped_size(), true /* read_only */); |
149 | 168 |
150 (*render_message_filter_ptr_) | 169 (*shared_bitmap_manager_ptr_) |
151 ->AllocatedSharedBitmap(std::move(buffer_handle), id); | 170 ->AllocatedSharedBitmap(std::move(buffer_handle), id); |
152 } | 171 } |
153 | 172 |
154 } // namespace content | 173 } // namespace display_compositor |
OLD | NEW |