OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "cc/test/test_shared_bitmap_manager.h" | 5 #include "cc/test/test_shared_bitmap_manager.h" |
6 | 6 |
7 #include "base/memory/shared_memory.h" | 7 #include "base/memory/shared_memory.h" |
8 | 8 |
9 namespace cc { | 9 namespace cc { |
10 | 10 |
11 namespace { | 11 namespace { |
12 class OwnedSharedBitmap : public SharedBitmap { | 12 class OwnedSharedBitmap : public SharedBitmap { |
13 public: | 13 public: |
14 OwnedSharedBitmap(scoped_ptr<base::SharedMemory> shared_memory, | 14 OwnedSharedBitmap(scoped_ptr<base::SharedMemory> shared_memory, |
15 const SharedBitmapId& id) | 15 const SharedBitmapId& id) |
16 : SharedBitmap(static_cast<uint8*>(shared_memory->memory()), id), | 16 : SharedBitmap(static_cast<uint8*>(shared_memory->memory()), id), |
17 shared_memory_(shared_memory.Pass()) {} | 17 shared_memory_(shared_memory.Pass()) {} |
18 | 18 |
19 ~OwnedSharedBitmap() override {} | 19 ~OwnedSharedBitmap() override {} |
20 | 20 |
21 base::SharedMemory* memory() override { return shared_memory_.get(); } | |
22 | |
23 private: | 21 private: |
24 scoped_ptr<base::SharedMemory> shared_memory_; | 22 scoped_ptr<base::SharedMemory> shared_memory_; |
25 }; | 23 }; |
26 | 24 |
27 class UnownedSharedBitmap : public SharedBitmap { | |
28 public: | |
29 UnownedSharedBitmap(base::SharedMemory* shared_memory, | |
30 const SharedBitmapId& id) | |
31 : SharedBitmap(static_cast<uint8*>(shared_memory->memory()), id), | |
32 shared_memory_(shared_memory) {} | |
33 | |
34 ~UnownedSharedBitmap() override {} | |
35 | |
36 base::SharedMemory* memory() override { return shared_memory_; } | |
37 | |
38 private: | |
39 base::SharedMemory* shared_memory_; | |
40 }; | |
41 } // namespace | 25 } // namespace |
42 | 26 |
43 TestSharedBitmapManager::TestSharedBitmapManager() {} | 27 TestSharedBitmapManager::TestSharedBitmapManager() {} |
44 | 28 |
45 TestSharedBitmapManager::~TestSharedBitmapManager() {} | 29 TestSharedBitmapManager::~TestSharedBitmapManager() {} |
46 | 30 |
47 scoped_ptr<SharedBitmap> TestSharedBitmapManager::AllocateSharedBitmap( | 31 scoped_ptr<SharedBitmap> TestSharedBitmapManager::AllocateSharedBitmap( |
48 const gfx::Size& size) { | 32 const gfx::Size& size) { |
49 base::AutoLock lock(lock_); | 33 base::AutoLock lock(lock_); |
50 scoped_ptr<base::SharedMemory> memory(new base::SharedMemory); | 34 scoped_ptr<base::SharedMemory> memory(new base::SharedMemory); |
51 memory->CreateAndMapAnonymous(size.GetArea() * 4); | 35 memory->CreateAndMapAnonymous(size.GetArea() * 4); |
52 SharedBitmapId id = SharedBitmap::GenerateId(); | 36 SharedBitmapId id = SharedBitmap::GenerateId(); |
53 bitmap_map_[id] = memory.get(); | 37 bitmap_map_[id] = memory.get(); |
54 return make_scoped_ptr(new OwnedSharedBitmap(memory.Pass(), id)); | 38 return make_scoped_ptr(new OwnedSharedBitmap(memory.Pass(), id)); |
55 } | 39 } |
56 | 40 |
57 scoped_ptr<SharedBitmap> TestSharedBitmapManager::GetSharedBitmapFromId( | 41 scoped_ptr<SharedBitmap> TestSharedBitmapManager::GetSharedBitmapFromId( |
58 const gfx::Size&, | 42 const gfx::Size&, |
59 const SharedBitmapId& id) { | 43 const SharedBitmapId& id) { |
60 base::AutoLock lock(lock_); | 44 base::AutoLock lock(lock_); |
61 if (bitmap_map_.find(id) == bitmap_map_.end()) | 45 if (bitmap_map_.find(id) == bitmap_map_.end()) |
62 return nullptr; | 46 return nullptr; |
63 return make_scoped_ptr(new UnownedSharedBitmap(bitmap_map_[id], id)); | 47 uint8* pixels = static_cast<uint8*>(bitmap_map_[id]->memory()); |
64 } | 48 return make_scoped_ptr(new SharedBitmap(pixels, id)); |
65 | |
66 scoped_ptr<SharedBitmap> TestSharedBitmapManager::GetBitmapForSharedMemory( | |
67 base::SharedMemory* memory) { | |
68 base::AutoLock lock(lock_); | |
69 SharedBitmapId id = SharedBitmap::GenerateId(); | |
70 bitmap_map_[id] = memory; | |
71 return make_scoped_ptr(new UnownedSharedBitmap(memory, id)); | |
72 } | 49 } |
73 | 50 |
74 } // namespace cc | 51 } // namespace cc |
OLD | NEW |