| OLD | NEW |
| (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 "components/display_compositor/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/strings/stringprintf.h" | |
| 17 #include "base/trace_event/process_memory_dump.h" | |
| 18 #include "build/build_config.h" | |
| 19 #include "mojo/public/cpp/system/platform_handle.h" | |
| 20 #include "ui/gfx/geometry/size.h" | |
| 21 | |
| 22 namespace display_compositor { | |
| 23 | |
| 24 class BitmapData : public base::RefCountedThreadSafe<BitmapData> { | |
| 25 public: | |
| 26 explicit BitmapData(size_t buffer_size) : buffer_size(buffer_size) {} | |
| 27 std::unique_ptr<base::SharedMemory> memory; | |
| 28 std::unique_ptr<uint8_t[]> pixels; | |
| 29 size_t buffer_size; | |
| 30 | |
| 31 private: | |
| 32 friend class base::RefCountedThreadSafe<BitmapData>; | |
| 33 ~BitmapData() {} | |
| 34 DISALLOW_COPY_AND_ASSIGN(BitmapData); | |
| 35 }; | |
| 36 | |
| 37 namespace { | |
| 38 | |
| 39 class HostSharedBitmap : public cc::SharedBitmap { | |
| 40 public: | |
| 41 HostSharedBitmap(uint8_t* pixels, | |
| 42 scoped_refptr<BitmapData> bitmap_data, | |
| 43 const cc::SharedBitmapId& id, | |
| 44 HostSharedBitmapManager* manager) | |
| 45 : SharedBitmap(pixels, id), | |
| 46 bitmap_data_(bitmap_data), | |
| 47 manager_(manager) {} | |
| 48 | |
| 49 ~HostSharedBitmap() override { | |
| 50 if (manager_) | |
| 51 manager_->FreeSharedMemoryFromMap(id()); | |
| 52 } | |
| 53 | |
| 54 private: | |
| 55 scoped_refptr<BitmapData> bitmap_data_; | |
| 56 HostSharedBitmapManager* manager_; | |
| 57 }; | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 base::LazyInstance<HostSharedBitmapManager>::DestructorAtExit | |
| 62 g_shared_memory_manager = LAZY_INSTANCE_INITIALIZER; | |
| 63 | |
| 64 HostSharedBitmapManagerClient::HostSharedBitmapManagerClient( | |
| 65 HostSharedBitmapManager* manager) | |
| 66 : manager_(manager), binding_(this) {} | |
| 67 | |
| 68 HostSharedBitmapManagerClient::~HostSharedBitmapManagerClient() { | |
| 69 for (const auto& id : owned_bitmaps_) | |
| 70 manager_->ChildDeletedSharedBitmap(id); | |
| 71 } | |
| 72 | |
| 73 void HostSharedBitmapManagerClient::Bind( | |
| 74 cc::mojom::SharedBitmapManagerAssociatedRequest request) { | |
| 75 binding_.Bind(std::move(request)); | |
| 76 } | |
| 77 | |
| 78 void HostSharedBitmapManagerClient::DidAllocateSharedBitmap( | |
| 79 mojo::ScopedSharedBufferHandle buffer, | |
| 80 const cc::SharedBitmapId& id) { | |
| 81 base::SharedMemoryHandle memory_handle; | |
| 82 size_t size; | |
| 83 MojoResult result = mojo::UnwrapSharedMemoryHandle( | |
| 84 std::move(buffer), &memory_handle, &size, NULL); | |
| 85 DCHECK_EQ(result, MOJO_RESULT_OK); | |
| 86 this->ChildAllocatedSharedBitmap(size, memory_handle, id); | |
| 87 } | |
| 88 | |
| 89 void HostSharedBitmapManagerClient::ChildAllocatedSharedBitmap( | |
| 90 size_t buffer_size, | |
| 91 const base::SharedMemoryHandle& handle, | |
| 92 const cc::SharedBitmapId& id) { | |
| 93 if (manager_->ChildAllocatedSharedBitmap(buffer_size, handle, id)) { | |
| 94 base::AutoLock lock(lock_); | |
| 95 owned_bitmaps_.insert(id); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void HostSharedBitmapManagerClient::DidDeleteSharedBitmap( | |
| 100 const cc::SharedBitmapId& id) { | |
| 101 manager_->ChildDeletedSharedBitmap(id); | |
| 102 { | |
| 103 base::AutoLock lock(lock_); | |
| 104 owned_bitmaps_.erase(id); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 HostSharedBitmapManager::HostSharedBitmapManager() {} | |
| 109 HostSharedBitmapManager::~HostSharedBitmapManager() { | |
| 110 DCHECK(handle_map_.empty()); | |
| 111 } | |
| 112 | |
| 113 HostSharedBitmapManager* HostSharedBitmapManager::current() { | |
| 114 return g_shared_memory_manager.Pointer(); | |
| 115 } | |
| 116 | |
| 117 std::unique_ptr<cc::SharedBitmap> HostSharedBitmapManager::AllocateSharedBitmap( | |
| 118 const gfx::Size& size) { | |
| 119 base::AutoLock lock(lock_); | |
| 120 size_t bitmap_size; | |
| 121 if (!cc::SharedBitmap::SizeInBytes(size, &bitmap_size)) | |
| 122 return std::unique_ptr<cc::SharedBitmap>(); | |
| 123 | |
| 124 scoped_refptr<BitmapData> data(new BitmapData(bitmap_size)); | |
| 125 // Bitmaps allocated in host don't need to be shared to other processes, so | |
| 126 // allocate them with new instead. | |
| 127 data->pixels = std::unique_ptr<uint8_t[]>(new uint8_t[bitmap_size]); | |
| 128 | |
| 129 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | |
| 130 handle_map_[id] = data; | |
| 131 return base::MakeUnique<HostSharedBitmap>(data->pixels.get(), data, id, this); | |
| 132 } | |
| 133 | |
| 134 std::unique_ptr<cc::SharedBitmap> | |
| 135 HostSharedBitmapManager::GetSharedBitmapFromId(const gfx::Size& size, | |
| 136 const cc::SharedBitmapId& id) { | |
| 137 base::AutoLock lock(lock_); | |
| 138 BitmapMap::iterator it = handle_map_.find(id); | |
| 139 if (it == handle_map_.end()) | |
| 140 return std::unique_ptr<cc::SharedBitmap>(); | |
| 141 | |
| 142 BitmapData* data = it->second.get(); | |
| 143 | |
| 144 size_t bitmap_size; | |
| 145 if (!cc::SharedBitmap::SizeInBytes(size, &bitmap_size) || | |
| 146 bitmap_size > data->buffer_size) | |
| 147 return std::unique_ptr<cc::SharedBitmap>(); | |
| 148 | |
| 149 if (data->pixels) { | |
| 150 return base::MakeUnique<HostSharedBitmap>(data->pixels.get(), data, id, | |
| 151 nullptr); | |
| 152 } | |
| 153 if (!data->memory->memory()) { | |
| 154 return std::unique_ptr<cc::SharedBitmap>(); | |
| 155 } | |
| 156 | |
| 157 return base::MakeUnique<HostSharedBitmap>( | |
| 158 static_cast<uint8_t*>(data->memory->memory()), data, id, nullptr); | |
| 159 } | |
| 160 | |
| 161 bool HostSharedBitmapManager::OnMemoryDump( | |
| 162 const base::trace_event::MemoryDumpArgs& args, | |
| 163 base::trace_event::ProcessMemoryDump* pmd) { | |
| 164 base::AutoLock lock(lock_); | |
| 165 | |
| 166 for (const auto& bitmap : handle_map_) { | |
| 167 base::trace_event::MemoryAllocatorDump* dump = | |
| 168 pmd->CreateAllocatorDump(base::StringPrintf( | |
| 169 "sharedbitmap/%s", | |
| 170 base::HexEncode(bitmap.first.name, sizeof(bitmap.first.name)) | |
| 171 .c_str())); | |
| 172 if (!dump) | |
| 173 return false; | |
| 174 | |
| 175 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
| 176 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | |
| 177 bitmap.second->buffer_size); | |
| 178 | |
| 179 // Generate a global GUID used to share this allocation with renderer | |
| 180 // processes. | |
| 181 auto guid = cc::GetSharedBitmapGUIDForTracing(bitmap.first); | |
| 182 pmd->CreateSharedGlobalAllocatorDump(guid); | |
| 183 pmd->AddOwnershipEdge(dump->guid(), guid); | |
| 184 } | |
| 185 | |
| 186 return true; | |
| 187 } | |
| 188 | |
| 189 bool HostSharedBitmapManager::ChildAllocatedSharedBitmap( | |
| 190 size_t buffer_size, | |
| 191 const base::SharedMemoryHandle& handle, | |
| 192 const cc::SharedBitmapId& id) { | |
| 193 base::AutoLock lock(lock_); | |
| 194 if (handle_map_.find(id) != handle_map_.end()) | |
| 195 return false; | |
| 196 scoped_refptr<BitmapData> data(new BitmapData(buffer_size)); | |
| 197 | |
| 198 handle_map_[id] = data; | |
| 199 data->memory = base::MakeUnique<base::SharedMemory>(handle, false); | |
| 200 data->memory->Map(data->buffer_size); | |
| 201 data->memory->Close(); | |
| 202 return true; | |
| 203 } | |
| 204 | |
| 205 void HostSharedBitmapManager::ChildDeletedSharedBitmap( | |
| 206 const cc::SharedBitmapId& id) { | |
| 207 base::AutoLock lock(lock_); | |
| 208 handle_map_.erase(id); | |
| 209 } | |
| 210 | |
| 211 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { | |
| 212 base::AutoLock lock(lock_); | |
| 213 return handle_map_.size(); | |
| 214 } | |
| 215 | |
| 216 void HostSharedBitmapManager::FreeSharedMemoryFromMap( | |
| 217 const cc::SharedBitmapId& id) { | |
| 218 base::AutoLock lock(lock_); | |
| 219 handle_map_.erase(id); | |
| 220 } | |
| 221 | |
| 222 } // namespace display_compositor | |
| OLD | NEW |