| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2017 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 "gpu/command_buffer/common/discardable_handle.h" |
| 6 #include "gpu/command_buffer/common/buffer.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace gpu { |
| 10 namespace { |
| 11 |
| 12 scoped_refptr<Buffer> MakeBufferForTesting(size_t num_handles) { |
| 13 size_t size = sizeof(base::subtle::Atomic32) * num_handles; |
| 14 std::unique_ptr<base::SharedMemory> shared_mem(new base::SharedMemory); |
| 15 shared_mem->CreateAndMapAnonymous(size); |
| 16 return MakeBufferFromSharedMemory(std::move(shared_mem), size); |
| 17 } |
| 18 |
| 19 } // namespace |
| 20 |
| 21 TEST(DiscardableHandleTest, BasicUsage) { |
| 22 scoped_refptr<Buffer> buffer = MakeBufferForTesting(1); |
| 23 |
| 24 uint32_t byte_offset = 0; |
| 25 int32_t shm_id = 1; |
| 26 DiscardableHandle handle; |
| 27 EXPECT_FALSE(handle.IsInitialized()); |
| 28 |
| 29 handle.InitializeWithNewData(buffer, byte_offset, shm_id); |
| 30 EXPECT_EQ(handle.shm_id(), shm_id); |
| 31 EXPECT_TRUE(handle.IsInitialized()); |
| 32 EXPECT_TRUE(handle.IsLockedForTesting()); |
| 33 |
| 34 EXPECT_FALSE(handle.Delete()); |
| 35 EXPECT_FALSE(handle.IsDeleted()); |
| 36 |
| 37 handle.Unlock(); |
| 38 EXPECT_FALSE(handle.IsLockedForTesting()); |
| 39 |
| 40 EXPECT_TRUE(handle.Lock()); |
| 41 EXPECT_TRUE(handle.IsLockedForTesting()); |
| 42 |
| 43 handle.Unlock(); |
| 44 EXPECT_FALSE(handle.IsLockedForTesting()); |
| 45 |
| 46 EXPECT_TRUE(handle.Delete()); |
| 47 EXPECT_TRUE(handle.IsDeleted()); |
| 48 EXPECT_FALSE(handle.IsLockedForTesting()); |
| 49 |
| 50 EXPECT_FALSE(handle.Lock()); |
| 51 EXPECT_TRUE(handle.IsDeleted()); |
| 52 EXPECT_FALSE(handle.IsLockedForTesting()); |
| 53 } |
| 54 |
| 55 TEST(DiscardableHandleTest, MultiLock) { |
| 56 scoped_refptr<Buffer> buffer = MakeBufferForTesting(1); |
| 57 |
| 58 uint32_t byte_offset = 0; |
| 59 int32_t shm_id = 1; |
| 60 DiscardableHandle handle; |
| 61 EXPECT_FALSE(handle.IsInitialized()); |
| 62 |
| 63 handle.InitializeWithNewData(buffer, byte_offset, shm_id); |
| 64 EXPECT_EQ(handle.shm_id(), shm_id); |
| 65 EXPECT_TRUE(handle.IsInitialized()); |
| 66 EXPECT_TRUE(handle.IsLockedForTesting()); |
| 67 |
| 68 for (int i = 1; i < 10; ++i) { |
| 69 EXPECT_TRUE(handle.IsLockedForTesting()); |
| 70 EXPECT_TRUE(handle.Lock()); |
| 71 } |
| 72 |
| 73 for (int i = 0; i < 10; ++i) { |
| 74 EXPECT_TRUE(handle.IsLockedForTesting()); |
| 75 handle.Unlock(); |
| 76 } |
| 77 |
| 78 EXPECT_FALSE(handle.IsLockedForTesting()); |
| 79 } |
| 80 |
| 81 TEST(DiscardableHandleTest, Suballocations) { |
| 82 static const size_t num_elements = 10; |
| 83 scoped_refptr<Buffer> buffer = MakeBufferForTesting(num_elements); |
| 84 |
| 85 DiscardableHandle handles[num_elements]; |
| 86 for (int i = 0; i < num_elements; ++i) { |
| 87 EXPECT_FALSE(handles[i].IsInitialized()); |
| 88 handles[i].InitializeWithNewData(buffer, sizeof(base::subtle::Atomic32) * i, |
| 89 i + 1); |
| 90 EXPECT_EQ(handles[i].shm_id(), i + 1); |
| 91 EXPECT_TRUE(handles[i].IsInitialized()); |
| 92 EXPECT_TRUE(handles[i].IsLockedForTesting()); |
| 93 } |
| 94 |
| 95 for (int i = 0; i < num_elements; i += 2) { |
| 96 handles[i].Unlock(); |
| 97 } |
| 98 |
| 99 for (int i = 1; i < num_elements; i += 2) { |
| 100 handles[i].Lock(); |
| 101 } |
| 102 |
| 103 for (int i = 0; i < num_elements; ++i) { |
| 104 if (i % 2) |
| 105 EXPECT_TRUE(handles[i].IsLockedForTesting()); |
| 106 else |
| 107 EXPECT_FALSE(handles[i].IsLockedForTesting()); |
| 108 } |
| 109 } |
| 110 |
| 111 TEST(DiscardableHandleTest, ExistingData) { |
| 112 scoped_refptr<Buffer> buffer = MakeBufferForTesting(1); |
| 113 |
| 114 uint32_t byte_offset = 0; |
| 115 int32_t shm_id = 1; |
| 116 DiscardableHandle init_handle; |
| 117 EXPECT_FALSE(init_handle.IsInitialized()); |
| 118 |
| 119 init_handle.InitializeWithNewData(buffer, byte_offset, shm_id); |
| 120 EXPECT_EQ(init_handle.shm_id(), shm_id); |
| 121 EXPECT_TRUE(init_handle.IsInitialized()); |
| 122 EXPECT_TRUE(init_handle.IsLockedForTesting()); |
| 123 |
| 124 init_handle.Unlock(); |
| 125 EXPECT_FALSE(init_handle.IsLockedForTesting()); |
| 126 |
| 127 DiscardableHandle handle; |
| 128 EXPECT_FALSE(handle.IsInitialized()); |
| 129 |
| 130 handle.InitializeWithExistingData(buffer, init_handle.byte_offset(), |
| 131 init_handle.shm_id()); |
| 132 EXPECT_EQ(handle.shm_id(), shm_id); |
| 133 EXPECT_TRUE(handle.IsInitialized()); |
| 134 EXPECT_FALSE(handle.IsLockedForTesting()); |
| 135 |
| 136 EXPECT_TRUE(handle.Delete()); |
| 137 EXPECT_TRUE(handle.IsDeleted()); |
| 138 EXPECT_TRUE(init_handle.IsDeleted()); |
| 139 } |
| 140 |
| 141 } // namespace gpu |
| OLD | NEW |