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

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

Issue 148243013: Add shared bitmap managers for browser and renderer processes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 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 | Annotate | Revision Log
OLDNEW
(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 size_t buffer_size)
19 : process_handle(process_handle),
20 memory_handle(memory_handle),
21 buffer_size(buffer_size) {}
22 base::ProcessHandle process_handle;
23 base::SharedMemoryHandle memory_handle;
24 scoped_ptr<base::SharedMemory> memory;
25 size_t buffer_size;
26
27 private:
28 friend class base::RefCountedThreadSafe<BitmapData>;
29 ~BitmapData() {}
30 DISALLOW_COPY_AND_ASSIGN(BitmapData);
31 };
32
33 // Holds a reference on the memory to keep it alive.
34 void FreeSharedMemory(scoped_refptr<BitmapData> data,
35 cc::SharedBitmap* bitmap) {}
36
37 base::LazyInstance<HostSharedBitmapManager> g_shared_memory_manager =
38 LAZY_INSTANCE_INITIALIZER;
39
40 HostSharedBitmapManager::HostSharedBitmapManager() {}
41 HostSharedBitmapManager::~HostSharedBitmapManager() {}
42
43 HostSharedBitmapManager* HostSharedBitmapManager::current() {
44 return g_shared_memory_manager.Pointer();
45 }
46
47 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::AllocateSharedBitmap(
48 const gfx::Size& size) {
49 // Bitmaps allocated in host don't need to be shared to other processes, so
50 // allocate them with new instead.
51 return scoped_ptr<cc::SharedBitmap>();
52 }
53
54 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::GetSharedBitmapFromId(
55 const gfx::Size& size,
56 const cc::SharedBitmapId& id) {
57 base::AutoLock lock(lock_);
58 BitmapMap::iterator it = handle_map_.find(id);
59 if (it == handle_map_.end())
60 return scoped_ptr<cc::SharedBitmap>();
61
62 BitmapData* data = it->second.get();
63
64 size_t bitmap_size = cc::SharedBitmap::GetSizeInBytes(size);
piman 2014/02/18 21:38:20 We probably want overflow protection here. Otherwi
65 if (bitmap_size > data->buffer_size)
66 return scoped_ptr<cc::SharedBitmap>();
67
68 if (!data->memory->memory()) {
69 TRACE_EVENT0("renderer_host",
70 "HostSharedBitmapManager::GetSharedBitmapFromId");
71 if (!data->memory->Map(data->buffer_size)) {
72 return scoped_ptr<cc::SharedBitmap>();
73 }
74 }
75
76 scoped_ptr<cc::SharedBitmap> bitmap(new cc::SharedBitmap(
77 data->memory.get(), id, base::Bind(&FreeSharedMemory, it->second)));
78
79 return bitmap.Pass();
80 }
81
82 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::GetBitmapForSharedMemory(
83 base::SharedMemory*) {
84 return scoped_ptr<cc::SharedBitmap>();
85 }
86
87 void HostSharedBitmapManager::ChildAllocatedSharedBitmap(
88 size_t buffer_size,
89 const base::SharedMemoryHandle& handle,
90 base::ProcessHandle process_handle,
91 const cc::SharedBitmapId& id) {
92 base::AutoLock lock(lock_);
93 if (handle_map_.find(id) != handle_map_.end())
94 return;
95 scoped_refptr<BitmapData> data(
96 new BitmapData(process_handle, handle, buffer_size));
97
98 handle_map_[id] = data;
99 process_map_[process_handle].insert(id);
100 #if defined(OS_WIN)
101 data->memory = make_scoped_ptr(
102 new base::SharedMemory(data->memory_handle, false, data->process_handle));
103 #else
104 data->memory =
105 make_scoped_ptr(new base::SharedMemory(data->memory_handle, false));
106 #endif
107 }
108
109 void HostSharedBitmapManager::AllocateSharedBitmapForChild(
110 base::ProcessHandle process_handle,
111 size_t buffer_size,
112 const cc::SharedBitmapId& id,
113 base::SharedMemoryHandle* shared_memory_handle) {
114 base::AutoLock lock(lock_);
115 if (handle_map_.find(id) != handle_map_.end()) {
116 *shared_memory_handle = base::SharedMemory::NULLHandle();
117 return;
118 }
119 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory);
120 if (!shared_memory->CreateAndMapAnonymous(buffer_size)) {
121 LOG(ERROR) << "Cannot create shared memory buffer";
122 *shared_memory_handle = base::SharedMemory::NULLHandle();
123 return;
124 }
125
126 scoped_refptr<BitmapData> data(
127 new BitmapData(process_handle, shared_memory->handle(), buffer_size));
128 data->memory = shared_memory.Pass();
129
130 handle_map_[id] = data;
131 process_map_[process_handle].insert(id);
132 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) {
133 LOG(ERROR) << "Cannot share shared memory buffer";
134 *shared_memory_handle = base::SharedMemory::NULLHandle();
135 return;
136 }
137 }
138
139 void HostSharedBitmapManager::ChildDeletedSharedBitmap(
140 const cc::SharedBitmapId& id) {
141 base::AutoLock lock(lock_);
142 BitmapMap::iterator it = handle_map_.find(id);
143 if (it == handle_map_.end())
144 return;
145 std::set<cc::SharedBitmapId>& res = process_map_[it->second->process_handle];
146 res.erase(id);
147 handle_map_.erase(it);
148 }
149
150 void HostSharedBitmapManager::ProcessRemoved(
151 base::ProcessHandle process_handle) {
152 base::AutoLock lock(lock_);
153 ProcessMap::iterator proc_it = process_map_.find(process_handle);
154 if (proc_it == process_map_.end())
155 return;
156 std::set<cc::SharedBitmapId>& res = proc_it->second;
157
158 for (std::set<cc::SharedBitmapId>::iterator it = res.begin(); it != res.end();
159 ++it) {
160 handle_map_.erase(*it);
161 }
162 process_map_.erase(proc_it);
163 }
164
165 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698