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