| OLD | NEW |
| 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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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) |
| 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 base::SharedMemoryTracker::AddOwnershipEdges( |
| 177 // processes. | 178 pmd, dump->guid(), bitmap.second->memory->handle(), |
| 178 auto guid = cc::GetSharedBitmapGUIDForTracing(bitmap.first); | 179 bitmap.second->memory->mapped_size()); |
| 179 pmd->CreateSharedGlobalAllocatorDump(guid); | |
| 180 pmd->AddOwnershipEdge(dump->guid(), guid); | |
| 181 } | 180 } |
| 182 | 181 |
| 183 return true; | 182 return true; |
| 184 } | 183 } |
| 185 | 184 |
| 186 bool HostSharedBitmapManager::ChildAllocatedSharedBitmap( | 185 bool HostSharedBitmapManager::ChildAllocatedSharedBitmap( |
| 187 size_t buffer_size, | 186 size_t buffer_size, |
| 188 const base::SharedMemoryHandle& handle, | 187 const base::SharedMemoryHandle& handle, |
| 189 const cc::SharedBitmapId& id) { | 188 const cc::SharedBitmapId& id) { |
| 190 base::AutoLock lock(lock_); | 189 base::AutoLock lock(lock_); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 218 | 217 |
| 219 scoped_refptr<BitmapData> data(new BitmapData(buffer_size)); | 218 scoped_refptr<BitmapData> data(new BitmapData(buffer_size)); |
| 220 data->memory = std::move(shared_memory); | 219 data->memory = std::move(shared_memory); |
| 221 | 220 |
| 222 handle_map_[id] = data; | 221 handle_map_[id] = data; |
| 223 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { | 222 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { |
| 224 LOG(ERROR) << "Cannot share shared memory buffer"; | 223 LOG(ERROR) << "Cannot share shared memory buffer"; |
| 225 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 224 *shared_memory_handle = base::SharedMemory::NULLHandle(); |
| 226 return; | 225 return; |
| 227 } | 226 } |
| 228 data->memory->Close(); | 227 data->memory->Close(); |
| 229 } | 228 } |
| 230 | 229 |
| 231 void HostSharedBitmapManager::ChildDeletedSharedBitmap( | 230 void HostSharedBitmapManager::ChildDeletedSharedBitmap( |
| 232 const cc::SharedBitmapId& id) { | 231 const cc::SharedBitmapId& id) { |
| 233 base::AutoLock lock(lock_); | 232 base::AutoLock lock(lock_); |
| 234 handle_map_.erase(id); | 233 handle_map_.erase(id); |
| 235 } | 234 } |
| 236 | 235 |
| 237 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { | 236 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { |
| 238 base::AutoLock lock(lock_); | 237 base::AutoLock lock(lock_); |
| 239 return handle_map_.size(); | 238 return handle_map_.size(); |
| 240 } | 239 } |
| 241 | 240 |
| 242 void HostSharedBitmapManager::FreeSharedMemoryFromMap( | 241 void HostSharedBitmapManager::FreeSharedMemoryFromMap( |
| 243 const cc::SharedBitmapId& id) { | 242 const cc::SharedBitmapId& id) { |
| 244 base::AutoLock lock(lock_); | 243 base::AutoLock lock(lock_); |
| 245 handle_map_.erase(id); | 244 handle_map_.erase(id); |
| 246 } | 245 } |
| 247 | 246 |
| 248 } // namespace content | 247 } // namespace content |
| OLD | NEW |