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

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.
danakj 2014/01/30 22:59:01 You have this file in content/common/. Would a ren
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 =
danakj 2014/01/30 22:59:01 Naive question maybe, as this seems to be the way
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 gfx::Size size) {
45 return scoped_ptr<cc::SharedBitmap>();
danakj 2014/01/30 22:59:01 NOTREACHED() and a comment?
46 }
47
48 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::GetSharedBitmapFromId(
49 gfx::Size size,
50 const cc::SharedBitmapId& id) {
51 base::AutoLock lock(lock_);
52 BitmapMap::iterator it = handle_map_.find(id);
53 if (it == handle_map_.end())
54 return scoped_ptr<cc::SharedBitmap>();
55
56 BitmapData* data = it->second.get();
57
58 size_t mapped_size = cc::SharedBitmap::GetSizeInBytes(size);
59 if (!data->memory->memory()) {
60 TRACE_EVENT0("renderer_host",
61 "HostSharedBitmapManager::GetSharedBitmapFromId");
62 if (!data->memory->Map(mapped_size)) {
63 return scoped_ptr<cc::SharedBitmap>();
64 }
65 } else if (data->memory->mapped_size() < mapped_size) {
66 return scoped_ptr<cc::SharedBitmap>();
67 }
68
69 scoped_ptr<cc::SharedBitmap> bitmap(new cc::SharedBitmap(
70 data->memory.get(), id, base::Bind(&FreeSharedMemory, it->second)));
71
72 return bitmap.Pass();
73 }
74
75 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::GetBitmapForSharedMemory(
76 base::SharedMemory*) {
77 return scoped_ptr<cc::SharedBitmap>();
78 }
79
80 void HostSharedBitmapManager::ChildAllocatedSharedBitmap(
81 const base::SharedMemoryHandle& handle,
82 base::ProcessHandle process_handle,
83 const cc::SharedBitmapId& id) {
84 base::AutoLock lock(lock_);
85 if (handle_map_.find(id) != handle_map_.end())
86 return;
87 scoped_refptr<BitmapData> data(new BitmapData(process_handle, handle));
88
89 handle_map_[id] = data;
90 process_map_[process_handle].insert(id);
91 #if defined(OS_WIN)
92 data->memory = make_scoped_ptr(
93 new base::SharedMemory(data->memory_handle, false, data->process_handle));
94 #else
95 data->memory =
96 make_scoped_ptr(new base::SharedMemory(data->memory_handle, false));
97 #endif
98 }
99
100 void HostSharedBitmapManager::AllocateSharedBitmapForChild(
101 base::ProcessHandle process_handle,
102 size_t buffer_size,
103 const cc::SharedBitmapId& id,
104 base::SharedMemoryHandle* shared_memory_handle) {
105 base::AutoLock lock(lock_);
106 if (handle_map_.find(id) != handle_map_.end()) {
107 *shared_memory_handle = base::SharedMemory::NULLHandle();
108 return;
109 }
110 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory);
111 if (!shared_memory->CreateAndMapAnonymous(buffer_size)) {
112 NOTREACHED() << "Cannot create shared memory buffer";
danakj 2014/01/30 22:59:01 Not a fan of NOTREACHED/DCHECK on a path that also
113 *shared_memory_handle = base::SharedMemory::NULLHandle();
114 return;
115 }
116
117 scoped_refptr<BitmapData> data(
118 new BitmapData(process_handle, shared_memory->handle()));
119 data->memory = shared_memory.Pass();
120
121 handle_map_[id] = data;
122 process_map_[process_handle].insert(id);
123 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) {
124 NOTREACHED() << "Cannot share shared memory buffer";
danakj 2014/01/30 22:59:01 Same here.
125 *shared_memory_handle = base::SharedMemory::NULLHandle();
126 return;
127 }
128 }
129
130 void HostSharedBitmapManager::ChildDeletedSharedBitmap(
131 const cc::SharedBitmapId& id) {
132 base::AutoLock lock(lock_);
133 BitmapMap::iterator it = handle_map_.find(id);
134 if (it == handle_map_.end())
135 return;
136 std::set<cc::SharedBitmapId>& res = process_map_[it->second->process_handle];
137 res.erase(id);
138 handle_map_.erase(it);
139 }
140
141 void HostSharedBitmapManager::ProcessRemoved(
142 base::ProcessHandle process_handle) {
143 base::AutoLock lock(lock_);
144 ProcessMap::iterator proc_it = process_map_.find(process_handle);
145 if (proc_it == process_map_.end())
146 return;
147 std::set<cc::SharedBitmapId>& res = proc_it->second;
148
149 for (std::set<cc::SharedBitmapId>::iterator it = res.begin(); it != res.end();
150 ++it) {
151 handle_map_.erase(*it);
152 }
153 process_map_.erase(proc_it);
154 }
155
156 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698