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