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

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

Issue 2775423003: Add ownership edges between HostSharedBitmap and shared memory
Patch Set: Add null checks for bitmaps 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
« no previous file with comments | « content/child/child_shared_bitmap_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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/common/host_shared_bitmap_manager.h" 5 #include "content/common/host_shared_bitmap_manager.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/shared_memory_tracker.h"
15 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
16 #include "base/trace_event/process_memory_dump.h" 17 #include "base/trace_event/process_memory_dump.h"
17 #include "build/build_config.h" 18 #include "build/build_config.h"
18 #include "content/common/view_messages.h" 19 #include "content/common/view_messages.h"
19 #include "ui/gfx/geometry/size.h" 20 #include "ui/gfx/geometry/size.h"
20 21
21 namespace content { 22 namespace content {
22 23
23 class BitmapData : public base::RefCountedThreadSafe<BitmapData> { 24 class BitmapData : public base::RefCountedThreadSafe<BitmapData> {
24 public: 25 public:
25 explicit BitmapData(size_t buffer_size) : buffer_size(buffer_size) {} 26 explicit BitmapData(size_t buffer_size) : buffer_size(buffer_size) {}
26 std::unique_ptr<base::SharedMemory> memory; 27 std::unique_ptr<base::SharedMemory> memory;
27 std::unique_ptr<uint8_t[]> pixels; 28 std::unique_ptr<uint8_t[]> pixels;
28 size_t buffer_size; 29 size_t buffer_size;
29 30
30 private: 31 private:
31 friend class base::RefCountedThreadSafe<BitmapData>; 32 friend class base::RefCountedThreadSafe<BitmapData>;
32 ~BitmapData() {} 33 ~BitmapData() {}
33 DISALLOW_COPY_AND_ASSIGN(BitmapData); 34 DISALLOW_COPY_AND_ASSIGN(BitmapData);
34 }; 35 };
35 36
36 namespace { 37 namespace {
37 38
38 class HostSharedBitmap : public cc::SharedBitmap { 39 class HostSharedBitmap : public cc::SharedBitmap {
39 public: 40 public:
40 HostSharedBitmap(uint8_t* pixels, 41 HostSharedBitmap(uint8_t* pixels,
41 scoped_refptr<BitmapData> bitmap_data, 42 scoped_refptr<BitmapData> bitmap_data,
42 const cc::SharedBitmapId& id, 43 const cc::SharedBitmapId& id,
43 HostSharedBitmapManager* manager) 44 HostSharedBitmapManager* manager)
44 : SharedBitmap(pixels, id), 45 : SharedBitmap(pixels, nullptr, id),
45 bitmap_data_(bitmap_data), 46 bitmap_data_(bitmap_data),
46 manager_(manager) {} 47 manager_(manager) {}
47 48
48 ~HostSharedBitmap() override { 49 ~HostSharedBitmap() override {
49 if (manager_) 50 if (manager_)
50 manager_->FreeSharedMemoryFromMap(id()); 51 manager_->FreeSharedMemoryFromMap(id());
51 } 52 }
52 53
53 private: 54 private:
54 scoped_refptr<BitmapData> bitmap_data_; 55 scoped_refptr<BitmapData> bitmap_data_;
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 const base::trace_event::MemoryDumpArgs& args, 160 const base::trace_event::MemoryDumpArgs& args,
160 base::trace_event::ProcessMemoryDump* pmd) { 161 base::trace_event::ProcessMemoryDump* pmd) {
161 base::AutoLock lock(lock_); 162 base::AutoLock lock(lock_);
162 163
163 for (const auto& bitmap : handle_map_) { 164 for (const auto& bitmap : handle_map_) {
164 base::trace_event::MemoryAllocatorDump* dump = 165 base::trace_event::MemoryAllocatorDump* dump =
165 pmd->CreateAllocatorDump(base::StringPrintf( 166 pmd->CreateAllocatorDump(base::StringPrintf(
166 "sharedbitmap/%s", 167 "sharedbitmap/%s",
167 base::HexEncode(bitmap.first.name, sizeof(bitmap.first.name)) 168 base::HexEncode(bitmap.first.name, sizeof(bitmap.first.name))
168 .c_str())); 169 .c_str()));
169 if (!dump) 170 if (!dump)
ssid 2017/03/31 00:25:21 I think you can remove this condition.
170 return false; 171 return false;
171 172
172 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, 173 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
173 base::trace_event::MemoryAllocatorDump::kUnitsBytes, 174 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
174 bitmap.second->buffer_size); 175 bitmap.second->buffer_size);
175 176
176 // Generate a global GUID used to share this allocation with renderer 177 if (bitmap.second->memory) {
ssid 2017/03/31 00:25:21 When would the shared_memory field be null? What h
177 // processes. 178 base::SharedMemoryTracker::AddOwnershipEdges(
178 auto guid = cc::GetSharedBitmapGUIDForTracing(bitmap.first); 179 pmd, dump->guid(), bitmap.second->memory->handle());
ssid 2017/03/31 00:25:21 You have updated only the browser side dumps to us
179 pmd->CreateSharedGlobalAllocatorDump(guid); 180 }
180 pmd->AddOwnershipEdge(dump->guid(), guid);
181 } 181 }
182 182
183 return true; 183 return true;
184 } 184 }
185 185
186 bool HostSharedBitmapManager::ChildAllocatedSharedBitmap( 186 bool HostSharedBitmapManager::ChildAllocatedSharedBitmap(
187 size_t buffer_size, 187 size_t buffer_size,
188 const base::SharedMemoryHandle& handle, 188 const base::SharedMemoryHandle& handle,
189 const cc::SharedBitmapId& id) { 189 const cc::SharedBitmapId& id) {
190 base::AutoLock lock(lock_); 190 base::AutoLock lock(lock_);
(...skipping 27 matching lines...) Expand all
218 218
219 scoped_refptr<BitmapData> data(new BitmapData(buffer_size)); 219 scoped_refptr<BitmapData> data(new BitmapData(buffer_size));
220 data->memory = std::move(shared_memory); 220 data->memory = std::move(shared_memory);
221 221
222 handle_map_[id] = data; 222 handle_map_[id] = data;
223 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { 223 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) {
224 LOG(ERROR) << "Cannot share shared memory buffer"; 224 LOG(ERROR) << "Cannot share shared memory buffer";
225 *shared_memory_handle = base::SharedMemory::NULLHandle(); 225 *shared_memory_handle = base::SharedMemory::NULLHandle();
226 return; 226 return;
227 } 227 }
228 data->memory->Close(); 228 data->memory->Close();
229 } 229 }
230 230
231 void HostSharedBitmapManager::ChildDeletedSharedBitmap( 231 void HostSharedBitmapManager::ChildDeletedSharedBitmap(
232 const cc::SharedBitmapId& id) { 232 const cc::SharedBitmapId& id) {
233 base::AutoLock lock(lock_); 233 base::AutoLock lock(lock_);
234 handle_map_.erase(id); 234 handle_map_.erase(id);
235 } 235 }
236 236
237 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { 237 size_t HostSharedBitmapManager::AllocatedBitmapCount() const {
238 base::AutoLock lock(lock_); 238 base::AutoLock lock(lock_);
239 return handle_map_.size(); 239 return handle_map_.size();
240 } 240 }
241 241
242 void HostSharedBitmapManager::FreeSharedMemoryFromMap( 242 void HostSharedBitmapManager::FreeSharedMemoryFromMap(
243 const cc::SharedBitmapId& id) { 243 const cc::SharedBitmapId& id) {
244 base::AutoLock lock(lock_); 244 base::AutoLock lock(lock_);
245 handle_map_.erase(id); 245 handle_map_.erase(id);
246 } 246 }
247 247
248 } // namespace content 248 } // namespace content
OLDNEW
« no previous file with comments | « content/child/child_shared_bitmap_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698