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 "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.h" |
10 #include "ui/gfx/size.h" | 10 #include "ui/gfx/size.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 | 13 |
14 class BitmapData : public base::RefCountedThreadSafe<BitmapData> { | 14 class BitmapData : public base::RefCountedThreadSafe<BitmapData> { |
15 public: | 15 public: |
16 BitmapData(base::ProcessHandle process_handle, | 16 BitmapData(base::ProcessHandle process_handle, |
17 base::SharedMemoryHandle memory_handle, | 17 base::SharedMemoryHandle memory_handle, |
18 size_t buffer_size) | 18 size_t buffer_size) |
19 : process_handle(process_handle), | 19 : process_handle(process_handle), |
20 memory_handle(memory_handle), | 20 memory_handle(memory_handle), |
21 buffer_size(buffer_size) {} | 21 buffer_size(buffer_size) {} |
22 base::ProcessHandle process_handle; | 22 base::ProcessHandle process_handle; |
23 base::SharedMemoryHandle memory_handle; | 23 base::SharedMemoryHandle memory_handle; |
24 scoped_ptr<base::SharedMemory> memory; | 24 scoped_ptr<base::SharedMemory> memory; |
| 25 scoped_ptr<uint8[]> pixels; |
25 size_t buffer_size; | 26 size_t buffer_size; |
26 | 27 |
27 private: | 28 private: |
28 friend class base::RefCountedThreadSafe<BitmapData>; | 29 friend class base::RefCountedThreadSafe<BitmapData>; |
29 ~BitmapData() {} | 30 ~BitmapData() {} |
30 DISALLOW_COPY_AND_ASSIGN(BitmapData); | 31 DISALLOW_COPY_AND_ASSIGN(BitmapData); |
31 }; | 32 }; |
32 | 33 |
33 // Holds a reference on the memory to keep it alive. | 34 // Holds a reference on the memory to keep it alive. |
34 void FreeSharedMemory(scoped_refptr<BitmapData> data, | 35 void FreeSharedMemory(scoped_refptr<BitmapData> data, |
35 cc::SharedBitmap* bitmap) {} | 36 cc::SharedBitmap* bitmap) {} |
36 | 37 |
37 base::LazyInstance<HostSharedBitmapManager> g_shared_memory_manager = | 38 base::LazyInstance<HostSharedBitmapManager> g_shared_memory_manager = |
38 LAZY_INSTANCE_INITIALIZER; | 39 LAZY_INSTANCE_INITIALIZER; |
39 | 40 |
40 HostSharedBitmapManager::HostSharedBitmapManager() {} | 41 HostSharedBitmapManager::HostSharedBitmapManager() {} |
41 HostSharedBitmapManager::~HostSharedBitmapManager() {} | 42 HostSharedBitmapManager::~HostSharedBitmapManager() {} |
42 | 43 |
43 HostSharedBitmapManager* HostSharedBitmapManager::current() { | 44 HostSharedBitmapManager* HostSharedBitmapManager::current() { |
44 return g_shared_memory_manager.Pointer(); | 45 return g_shared_memory_manager.Pointer(); |
45 } | 46 } |
46 | 47 |
47 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::AllocateSharedBitmap( | 48 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::AllocateSharedBitmap( |
48 const gfx::Size& size) { | 49 const gfx::Size& size) { |
| 50 base::AutoLock lock(lock_); |
| 51 size_t bitmap_size; |
| 52 if (!cc::SharedBitmap::SizeInBytes(size, &bitmap_size)) |
| 53 return scoped_ptr<cc::SharedBitmap>(); |
| 54 |
| 55 scoped_refptr<BitmapData> data( |
| 56 new BitmapData(base::GetCurrentProcessHandle(), |
| 57 base::SharedMemory::NULLHandle(), |
| 58 bitmap_size)); |
49 // Bitmaps allocated in host don't need to be shared to other processes, so | 59 // Bitmaps allocated in host don't need to be shared to other processes, so |
50 // allocate them with new instead. | 60 // allocate them with new instead. |
51 return scoped_ptr<cc::SharedBitmap>(); | 61 data->pixels = scoped_ptr<uint8[]>(new uint8[bitmap_size]); |
| 62 |
| 63 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); |
| 64 handle_map_[id] = data; |
| 65 return make_scoped_ptr(new cc::SharedBitmap( |
| 66 data->pixels.get(), id, base::Bind(&FreeSharedMemory, data))); |
52 } | 67 } |
53 | 68 |
54 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::GetSharedBitmapFromId( | 69 scoped_ptr<cc::SharedBitmap> HostSharedBitmapManager::GetSharedBitmapFromId( |
55 const gfx::Size& size, | 70 const gfx::Size& size, |
56 const cc::SharedBitmapId& id) { | 71 const cc::SharedBitmapId& id) { |
57 base::AutoLock lock(lock_); | 72 base::AutoLock lock(lock_); |
58 BitmapMap::iterator it = handle_map_.find(id); | 73 BitmapMap::iterator it = handle_map_.find(id); |
59 if (it == handle_map_.end()) | 74 if (it == handle_map_.end()) |
60 return scoped_ptr<cc::SharedBitmap>(); | 75 return scoped_ptr<cc::SharedBitmap>(); |
61 | 76 |
62 BitmapData* data = it->second.get(); | 77 BitmapData* data = it->second.get(); |
63 | 78 |
64 size_t bitmap_size; | 79 size_t bitmap_size; |
65 if (!cc::SharedBitmap::SizeInBytes(size, &bitmap_size) || | 80 if (!cc::SharedBitmap::SizeInBytes(size, &bitmap_size) || |
66 bitmap_size > data->buffer_size) | 81 bitmap_size > data->buffer_size) |
67 return scoped_ptr<cc::SharedBitmap>(); | 82 return scoped_ptr<cc::SharedBitmap>(); |
68 | 83 |
| 84 if (data->pixels) { |
| 85 return make_scoped_ptr(new cc::SharedBitmap( |
| 86 data->pixels.get(), id, base::Bind(&FreeSharedMemory, it->second))); |
| 87 } |
69 if (!data->memory->memory()) { | 88 if (!data->memory->memory()) { |
70 TRACE_EVENT0("renderer_host", | 89 TRACE_EVENT0("renderer_host", |
71 "HostSharedBitmapManager::GetSharedBitmapFromId"); | 90 "HostSharedBitmapManager::GetSharedBitmapFromId"); |
72 if (!data->memory->Map(data->buffer_size)) { | 91 if (!data->memory->Map(data->buffer_size)) { |
73 return scoped_ptr<cc::SharedBitmap>(); | 92 return scoped_ptr<cc::SharedBitmap>(); |
74 } | 93 } |
75 } | 94 } |
76 | 95 |
77 scoped_ptr<cc::SharedBitmap> bitmap(new cc::SharedBitmap( | 96 scoped_ptr<cc::SharedBitmap> bitmap(new cc::SharedBitmap( |
78 data->memory.get(), id, base::Bind(&FreeSharedMemory, it->second))); | 97 data->memory.get(), id, base::Bind(&FreeSharedMemory, it->second))); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 | 178 |
160 for (base::hash_set<cc::SharedBitmapId>::iterator it = res.begin(); | 179 for (base::hash_set<cc::SharedBitmapId>::iterator it = res.begin(); |
161 it != res.end(); | 180 it != res.end(); |
162 ++it) { | 181 ++it) { |
163 handle_map_.erase(*it); | 182 handle_map_.erase(*it); |
164 } | 183 } |
165 process_map_.erase(proc_it); | 184 process_map_.erase(proc_it); |
166 } | 185 } |
167 | 186 |
168 } // namespace content | 187 } // namespace content |
OLD | NEW |