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

Side by Side Diff: content/common/host_shared_bitmap_manager.cc

Issue 2717213004: Move SharedBitmapManager implementation out of content/ (Closed)
Patch Set: rebase Created 3 years, 8 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 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/common/host_shared_bitmap_manager.h"
6
7 #include <stdint.h>
8
9 #include <utility>
10
11 #include "base/lazy_instance.h"
12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/trace_event/process_memory_dump.h"
17 #include "build/build_config.h"
18 #include "content/common/view_messages.h"
19 #include "ui/gfx/geometry/size.h"
20
21 namespace content {
22
23 class BitmapData : public base::RefCountedThreadSafe<BitmapData> {
24 public:
25 explicit BitmapData(size_t buffer_size) : buffer_size(buffer_size) {}
26 std::unique_ptr<base::SharedMemory> memory;
27 std::unique_ptr<uint8_t[]> pixels;
28 size_t buffer_size;
29
30 private:
31 friend class base::RefCountedThreadSafe<BitmapData>;
32 ~BitmapData() {}
33 DISALLOW_COPY_AND_ASSIGN(BitmapData);
34 };
35
36 namespace {
37
38 class HostSharedBitmap : public cc::SharedBitmap {
39 public:
40 HostSharedBitmap(uint8_t* pixels,
41 scoped_refptr<BitmapData> bitmap_data,
42 const cc::SharedBitmapId& id,
43 HostSharedBitmapManager* manager)
44 : SharedBitmap(pixels, id),
45 bitmap_data_(bitmap_data),
46 manager_(manager) {}
47
48 ~HostSharedBitmap() override {
49 if (manager_)
50 manager_->FreeSharedMemoryFromMap(id());
51 }
52
53 private:
54 scoped_refptr<BitmapData> bitmap_data_;
55 HostSharedBitmapManager* manager_;
56 };
57
58 } // namespace
59
60 base::LazyInstance<HostSharedBitmapManager>::DestructorAtExit
61 g_shared_memory_manager = LAZY_INSTANCE_INITIALIZER;
62
63 HostSharedBitmapManagerClient::HostSharedBitmapManagerClient(
64 HostSharedBitmapManager* manager)
65 : manager_(manager) {
66 }
67
68 HostSharedBitmapManagerClient::~HostSharedBitmapManagerClient() {
69 for (const auto& id : owned_bitmaps_)
70 manager_->ChildDeletedSharedBitmap(id);
71 }
72
73 void HostSharedBitmapManagerClient::AllocateSharedBitmapForChild(
74 base::ProcessHandle process_handle,
75 size_t buffer_size,
76 const cc::SharedBitmapId& id,
77 base::SharedMemoryHandle* shared_memory_handle) {
78 manager_->AllocateSharedBitmapForChild(process_handle, buffer_size, id,
79 shared_memory_handle);
80 if (*shared_memory_handle != base::SharedMemory::NULLHandle()) {
81 base::AutoLock lock(lock_);
82 owned_bitmaps_.insert(id);
83 }
84 }
85
86 void HostSharedBitmapManagerClient::ChildAllocatedSharedBitmap(
87 size_t buffer_size,
88 const base::SharedMemoryHandle& handle,
89 const cc::SharedBitmapId& id) {
90 if (manager_->ChildAllocatedSharedBitmap(buffer_size, handle, id)) {
91 base::AutoLock lock(lock_);
92 owned_bitmaps_.insert(id);
93 }
94 }
95
96 void HostSharedBitmapManagerClient::ChildDeletedSharedBitmap(
97 const cc::SharedBitmapId& id) {
98 manager_->ChildDeletedSharedBitmap(id);
99 {
100 base::AutoLock lock(lock_);
101 owned_bitmaps_.erase(id);
102 }
103 }
104
105 HostSharedBitmapManager::HostSharedBitmapManager() {}
106 HostSharedBitmapManager::~HostSharedBitmapManager() {
107 DCHECK(handle_map_.empty());
108 }
109
110 HostSharedBitmapManager* HostSharedBitmapManager::current() {
111 return g_shared_memory_manager.Pointer();
112 }
113
114 std::unique_ptr<cc::SharedBitmap> HostSharedBitmapManager::AllocateSharedBitmap(
115 const gfx::Size& size) {
116 base::AutoLock lock(lock_);
117 size_t bitmap_size;
118 if (!cc::SharedBitmap::SizeInBytes(size, &bitmap_size))
119 return std::unique_ptr<cc::SharedBitmap>();
120
121 scoped_refptr<BitmapData> data(new BitmapData(bitmap_size));
122 // Bitmaps allocated in host don't need to be shared to other processes, so
123 // allocate them with new instead.
124 data->pixels = std::unique_ptr<uint8_t[]>(new uint8_t[bitmap_size]);
125
126 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
127 handle_map_[id] = data;
128 return base::MakeUnique<HostSharedBitmap>(data->pixels.get(), data, id, this);
129 }
130
131 std::unique_ptr<cc::SharedBitmap>
132 HostSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size& size,
133 const cc::SharedBitmapId& id) {
134 base::AutoLock lock(lock_);
135 BitmapMap::iterator it = handle_map_.find(id);
136 if (it == handle_map_.end())
137 return std::unique_ptr<cc::SharedBitmap>();
138
139 BitmapData* data = it->second.get();
140
141 size_t bitmap_size;
142 if (!cc::SharedBitmap::SizeInBytes(size, &bitmap_size) ||
143 bitmap_size > data->buffer_size)
144 return std::unique_ptr<cc::SharedBitmap>();
145
146 if (data->pixels) {
147 return base::MakeUnique<HostSharedBitmap>(data->pixels.get(), data, id,
148 nullptr);
149 }
150 if (!data->memory->memory()) {
151 return std::unique_ptr<cc::SharedBitmap>();
152 }
153
154 return base::MakeUnique<HostSharedBitmap>(
155 static_cast<uint8_t*>(data->memory->memory()), data, id, nullptr);
156 }
157
158 bool HostSharedBitmapManager::OnMemoryDump(
159 const base::trace_event::MemoryDumpArgs& args,
160 base::trace_event::ProcessMemoryDump* pmd) {
161 base::AutoLock lock(lock_);
162
163 for (const auto& bitmap : handle_map_) {
164 base::trace_event::MemoryAllocatorDump* dump =
165 pmd->CreateAllocatorDump(base::StringPrintf(
166 "sharedbitmap/%s",
167 base::HexEncode(bitmap.first.name, sizeof(bitmap.first.name))
168 .c_str()));
169 if (!dump)
170 return false;
171
172 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
173 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
174 bitmap.second->buffer_size);
175
176 // Generate a global GUID used to share this allocation with renderer
177 // processes.
178 auto guid = cc::GetSharedBitmapGUIDForTracing(bitmap.first);
179 pmd->CreateSharedGlobalAllocatorDump(guid);
180 pmd->AddOwnershipEdge(dump->guid(), guid);
181 }
182
183 return true;
184 }
185
186 bool HostSharedBitmapManager::ChildAllocatedSharedBitmap(
187 size_t buffer_size,
188 const base::SharedMemoryHandle& handle,
189 const cc::SharedBitmapId& id) {
190 base::AutoLock lock(lock_);
191 if (handle_map_.find(id) != handle_map_.end())
192 return false;
193 scoped_refptr<BitmapData> data(new BitmapData(buffer_size));
194
195 handle_map_[id] = data;
196 data->memory = base::MakeUnique<base::SharedMemory>(handle, false);
197 data->memory->Map(data->buffer_size);
198 data->memory->Close();
199 return true;
200 }
201
202 void HostSharedBitmapManager::AllocateSharedBitmapForChild(
203 base::ProcessHandle process_handle,
204 size_t buffer_size,
205 const cc::SharedBitmapId& id,
206 base::SharedMemoryHandle* shared_memory_handle) {
207 base::AutoLock lock(lock_);
208 if (handle_map_.find(id) != handle_map_.end()) {
209 *shared_memory_handle = base::SharedMemory::NULLHandle();
210 return;
211 }
212 std::unique_ptr<base::SharedMemory> shared_memory(new base::SharedMemory);
213 if (!shared_memory->CreateAndMapAnonymous(buffer_size)) {
214 LOG(ERROR) << "Cannot create shared memory buffer";
215 *shared_memory_handle = base::SharedMemory::NULLHandle();
216 return;
217 }
218
219 scoped_refptr<BitmapData> data(new BitmapData(buffer_size));
220 data->memory = std::move(shared_memory);
221
222 handle_map_[id] = data;
223 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) {
224 LOG(ERROR) << "Cannot share shared memory buffer";
225 *shared_memory_handle = base::SharedMemory::NULLHandle();
226 return;
227 }
228 data->memory->Close();
229 }
230
231 void HostSharedBitmapManager::ChildDeletedSharedBitmap(
232 const cc::SharedBitmapId& id) {
233 base::AutoLock lock(lock_);
234 handle_map_.erase(id);
235 }
236
237 size_t HostSharedBitmapManager::AllocatedBitmapCount() const {
238 base::AutoLock lock(lock_);
239 return handle_map_.size();
240 }
241
242 void HostSharedBitmapManager::FreeSharedMemoryFromMap(
243 const cc::SharedBitmapId& id) {
244 base::AutoLock lock(lock_);
245 handle_map_.erase(id);
246 }
247
248 } // namespace content
OLDNEW
« no previous file with comments | « content/common/host_shared_bitmap_manager.h ('k') | content/common/host_shared_bitmap_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698