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

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

Issue 2717213004: Move SharedBitmapManager implementation out of content/ (Closed)
Patch Set: rebase Created 3 years, 9 months 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/child/child_shared_bitmap_manager.h"
6
7 #include <stddef.h>
8
9 #include <utility>
10
11 #include "base/debug/alias.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/process/memory.h"
14 #include "base/process/process_metrics.h"
15 #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"
19 #include "ui/gfx/geometry/size.h"
20
21 namespace content {
22
23 namespace {
24
25 class ChildSharedBitmap : public SharedMemoryBitmap {
26 public:
27 ChildSharedBitmap(
28 const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>&
29 render_message_filter_ptr,
30 base::SharedMemory* shared_memory,
31 const cc::SharedBitmapId& id)
32 : SharedMemoryBitmap(static_cast<uint8_t*>(shared_memory->memory()),
33 id,
34 shared_memory),
35 render_message_filter_ptr_(render_message_filter_ptr) {}
36
37 ChildSharedBitmap(
38 const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>&
39 render_message_filter_ptr,
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) {
45 shared_memory_holder_ = std::move(shared_memory_holder);
46 }
47
48 ~ChildSharedBitmap() override {
49 (*render_message_filter_ptr_)->DeletedSharedBitmap(id());
50 }
51
52 private:
53 scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>
54 render_message_filter_ptr_;
55 std::unique_ptr<base::SharedMemory> shared_memory_holder_;
56 };
57
58 // Collect extra information for debugging bitmap creation failures.
59 void CollectMemoryUsageAndDie(const gfx::Size& size, size_t alloc_size) {
60 #if defined(OS_WIN)
61 int width = size.width();
62 int height = size.height();
63 DWORD last_error = GetLastError();
64
65 std::unique_ptr<base::ProcessMetrics> metrics(
66 base::ProcessMetrics::CreateProcessMetrics(
67 base::GetCurrentProcessHandle()));
68
69 size_t private_bytes = 0;
70 size_t shared_bytes = 0;
71 metrics->GetMemoryBytes(&private_bytes, &shared_bytes);
72
73 base::debug::Alias(&width);
74 base::debug::Alias(&height);
75 base::debug::Alias(&last_error);
76 base::debug::Alias(&private_bytes);
77 base::debug::Alias(&shared_bytes);
78 #endif
79 base::TerminateBecauseOutOfMemory(alloc_size);
80 }
81
82 } // namespace
83
84 SharedMemoryBitmap::SharedMemoryBitmap(uint8_t* pixels,
85 const cc::SharedBitmapId& id,
86 base::SharedMemory* shared_memory)
87 : SharedBitmap(pixels, id), shared_memory_(shared_memory) {}
88
89 ChildSharedBitmapManager::ChildSharedBitmapManager(
90 const scoped_refptr<mojom::ThreadSafeRenderMessageFilterAssociatedPtr>&
91 render_message_filter_ptr)
92 : render_message_filter_ptr_(render_message_filter_ptr) {}
93
94 ChildSharedBitmapManager::~ChildSharedBitmapManager() {}
95
96 std::unique_ptr<cc::SharedBitmap>
97 ChildSharedBitmapManager::AllocateSharedBitmap(const gfx::Size& size) {
98 TRACE_EVENT2("renderer",
99 "ChildSharedBitmapManager::AllocateSharedMemoryBitmap", "width",
100 size.width(), "height", size.height());
101 size_t memory_size;
102 if (!cc::SharedBitmap::SizeInBytes(size, &memory_size))
103 return std::unique_ptr<SharedMemoryBitmap>();
104 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
105 std::unique_ptr<base::SharedMemory> memory =
106 ChildThreadImpl::AllocateSharedMemory(memory_size);
107 if (!memory || !memory->Map(memory_size))
108 CollectMemoryUsageAndDie(size, memory_size);
109
110 NotifyAllocatedSharedBitmap(memory.get(), id);
111
112 // Close the associated FD to save resources, the previously mapped memory
113 // remains available.
114 memory->Close();
115
116 return base::MakeUnique<ChildSharedBitmap>(render_message_filter_ptr_,
117 std::move(memory), id);
118 }
119
120 std::unique_ptr<cc::SharedBitmap>
121 ChildSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size&,
122 const cc::SharedBitmapId&) {
123 NOTREACHED();
124 return std::unique_ptr<cc::SharedBitmap>();
125 }
126
127 std::unique_ptr<cc::SharedBitmap>
128 ChildSharedBitmapManager::GetBitmapForSharedMemory(base::SharedMemory* mem) {
129 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
130 NotifyAllocatedSharedBitmap(mem, cc::SharedBitmap::GenerateId());
131 return base::MakeUnique<ChildSharedBitmap>(render_message_filter_ptr_,
132 std::move(mem), id);
133 }
134
135 // Notifies the browser process that a shared bitmap with the given ID was
136 // allocated. Caller keeps ownership of |memory|.
137 void ChildSharedBitmapManager::NotifyAllocatedSharedBitmap(
138 base::SharedMemory* memory,
139 const cc::SharedBitmapId& id) {
140 base::SharedMemoryHandle handle_to_send =
141 base::SharedMemory::DuplicateHandle(memory->handle());
142 if (!base::SharedMemory::IsHandleValid(handle_to_send)) {
143 LOG(ERROR) << "Failed to duplicate shared memory handle for bitmap.";
144 return;
145 }
146
147 mojo::ScopedSharedBufferHandle buffer_handle = mojo::WrapSharedMemoryHandle(
148 handle_to_send, memory->mapped_size(), true /* read_only */);
149
150 (*render_message_filter_ptr_)
151 ->AllocatedSharedBitmap(std::move(buffer_handle), id);
152 }
153
154 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698