Chromium Code Reviews| 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 "content/common/host_shared_bitmap_manager.h" | |
| 6 | |
| 7 #include "base/lazy_instance.h" | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 #include "content/common/view_messages.h" | |
| 10 #include "ui/gfx/size.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 class BitmapData : public base::RefCountedThreadSafe<BitmapData> { | |
| 15 public: | |
| 16 BitmapData(base::ProcessHandle process_handle, | |
| 17 base::SharedMemoryHandle memory_handle) | |
| 18 : process_handle(process_handle), memory_handle(memory_handle) {} | |
| 19 base::ProcessHandle process_handle; | |
| 20 base::SharedMemoryHandle memory_handle; | |
| 21 scoped_ptr<base::SharedMemory> memory; | |
| 22 | |
| 23 private: | |
| 24 friend class base::RefCountedThreadSafe<BitmapData>; | |
| 25 ~BitmapData() {} | |
| 26 DISALLOW_COPY_AND_ASSIGN(BitmapData); | |
| 27 }; | |
| 28 | |
| 29 // Holds a reference on the memory to keep it alive. | |
| 30 void FreeSharedMemory(scoped_refptr<BitmapData> data, | |
| 31 cc::SharedBitmap* bitmap) {} | |
| 32 | |
| 33 base::LazyInstance<HostSharedBitmapManager> g_shared_memory_manager = | |
| 34 LAZY_INSTANCE_INITIALIZER; | |
| 35 | |
| 36 HostSharedBitmapManager::HostSharedBitmapManager() {} | |
| 37 HostSharedBitmapManager::~HostSharedBitmapManager() {} | |
| 38 | |
| 39 HostSharedBitmapManager* HostSharedBitmapManager::current() { | |
| 40 return g_shared_memory_manager.Pointer(); | |
| 41 } | |
| 42 | |
| 43 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::AllocateSharedBitmap( | |
| 44 const gfx::Size& size) { | |
| 45 // Bitmaps allocated in host don't need to be shared to other processes, so | |
|
danakj
2014/02/14 20:16:48
Ah ok. This causes RP to go down the new[] path in
| |
| 46 // allocate them with new instead. | |
| 47 return scoped_ptr<cc::SharedBitmap>(); | |
| 48 } | |
| 49 | |
| 50 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::GetSharedBitmapFromId( | |
| 51 const gfx::Size& size, | |
| 52 const cc::SharedBitmapId& id) { | |
| 53 base::AutoLock lock(lock_); | |
| 54 BitmapMap::iterator it = handle_map_.find(id); | |
| 55 if (it == handle_map_.end()) | |
| 56 return scoped_ptr<cc::SharedBitmap>(); | |
| 57 | |
| 58 BitmapData* data = it->second.get(); | |
| 59 | |
| 60 size_t mapped_size = cc::SharedBitmap::GetSizeInBytes(size); | |
| 61 if (!data->memory->memory()) { | |
| 62 TRACE_EVENT0("renderer_host", | |
| 63 "HostSharedBitmapManager::GetSharedBitmapFromId"); | |
| 64 if (!data->memory->Map(mapped_size)) { | |
| 65 return scoped_ptr<cc::SharedBitmap>(); | |
| 66 } | |
| 67 } else if (data->memory->mapped_size() < mapped_size) { | |
| 68 return scoped_ptr<cc::SharedBitmap>(); | |
| 69 } | |
| 70 | |
| 71 scoped_ptr<cc::SharedBitmap> bitmap(new cc::SharedBitmap( | |
| 72 data->memory.get(), id, base::Bind(&FreeSharedMemory, it->second))); | |
| 73 | |
| 74 return bitmap.Pass(); | |
| 75 } | |
| 76 | |
| 77 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::GetBitmapForSharedMemory( | |
| 78 base::SharedMemory*) { | |
| 79 return scoped_ptr<cc::SharedBitmap>(); | |
|
danakj
2014/02/14 20:16:48
Can this NOTREACHED()? This would mean the browser
jbauman
2014/02/18 19:31:35
Sure.
| |
| 80 } | |
| 81 | |
| 82 void HostSharedBitmapManager::ChildAllocatedSharedBitmap( | |
| 83 const base::SharedMemoryHandle& handle, | |
| 84 base::ProcessHandle process_handle, | |
| 85 const cc::SharedBitmapId& id) { | |
| 86 base::AutoLock lock(lock_); | |
| 87 if (handle_map_.find(id) != handle_map_.end()) | |
| 88 return; | |
| 89 scoped_refptr<BitmapData> data(new BitmapData(process_handle, handle)); | |
| 90 | |
| 91 handle_map_[id] = data; | |
| 92 process_map_[process_handle].insert(id); | |
| 93 #if defined(OS_WIN) | |
| 94 data->memory = make_scoped_ptr( | |
| 95 new base::SharedMemory(data->memory_handle, false, data->process_handle)); | |
| 96 #else | |
| 97 data->memory = | |
| 98 make_scoped_ptr(new base::SharedMemory(data->memory_handle, false)); | |
| 99 #endif | |
| 100 } | |
| 101 | |
| 102 void HostSharedBitmapManager::AllocateSharedBitmapForChild( | |
| 103 base::ProcessHandle process_handle, | |
| 104 size_t buffer_size, | |
| 105 const cc::SharedBitmapId& id, | |
| 106 base::SharedMemoryHandle* shared_memory_handle) { | |
| 107 base::AutoLock lock(lock_); | |
| 108 if (handle_map_.find(id) != handle_map_.end()) { | |
| 109 *shared_memory_handle = base::SharedMemory::NULLHandle(); | |
| 110 return; | |
| 111 } | |
| 112 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); | |
| 113 if (!shared_memory->CreateAndMapAnonymous(buffer_size)) { | |
| 114 LOG(ERROR) << "Cannot create shared memory buffer"; | |
| 115 *shared_memory_handle = base::SharedMemory::NULLHandle(); | |
| 116 return; | |
| 117 } | |
| 118 | |
| 119 scoped_refptr<BitmapData> data( | |
| 120 new BitmapData(process_handle, shared_memory->handle())); | |
| 121 data->memory = shared_memory.Pass(); | |
| 122 | |
| 123 handle_map_[id] = data; | |
| 124 process_map_[process_handle].insert(id); | |
| 125 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { | |
| 126 LOG(ERROR) << "Cannot share shared memory buffer"; | |
| 127 *shared_memory_handle = base::SharedMemory::NULLHandle(); | |
| 128 return; | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 void HostSharedBitmapManager::ChildDeletedSharedBitmap( | |
| 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; | |
| 138 std::set<cc::SharedBitmapId>& res = process_map_[it->second->process_handle]; | |
| 139 res.erase(id); | |
| 140 handle_map_.erase(it); | |
| 141 } | |
| 142 | |
| 143 void HostSharedBitmapManager::ProcessRemoved( | |
| 144 base::ProcessHandle process_handle) { | |
| 145 base::AutoLock lock(lock_); | |
| 146 ProcessMap::iterator proc_it = process_map_.find(process_handle); | |
| 147 if (proc_it == process_map_.end()) | |
| 148 return; | |
| 149 std::set<cc::SharedBitmapId>& res = proc_it->second; | |
| 150 | |
| 151 for (std::set<cc::SharedBitmapId>::iterator it = res.begin(); it != res.end(); | |
| 152 ++it) { | |
| 153 handle_map_.erase(*it); | |
| 154 } | |
| 155 process_map_.erase(proc_it); | |
| 156 } | |
| 157 | |
| 158 } // namespace content | |
| OLD | NEW |