Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 1 | #include <errno.h> |
Fan Xu | cfbe074 | 2018-11-21 15:03:32 -0800 | [diff] [blame] | 2 | |
| 3 | #include <bufferhub/BufferNode.h> |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 4 | #include <gmock/gmock.h> |
| 5 | #include <gtest/gtest.h> |
Fan Xu | d34a80a | 2018-12-04 11:32:39 -0800 | [diff] [blame] | 6 | #include <ui/BufferHubDefs.h> |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 7 | #include <ui/GraphicBufferMapper.h> |
| 8 | |
| 9 | namespace android { |
| 10 | namespace frameworks { |
| 11 | namespace bufferhub { |
| 12 | namespace V1_0 { |
| 13 | namespace implementation { |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | using testing::NotNull; |
| 18 | |
| 19 | const uint32_t kWidth = 640; |
| 20 | const uint32_t kHeight = 480; |
| 21 | const uint32_t kLayerCount = 1; |
| 22 | const uint32_t kFormat = 1; |
| 23 | const uint64_t kUsage = 0; |
| 24 | const size_t kUserMetadataSize = 0; |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 25 | |
| 26 | class BufferNodeTest : public ::testing::Test { |
| 27 | protected: |
| 28 | void SetUp() override { |
| 29 | buffer_node = |
| 30 | new BufferNode(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize); |
| 31 | ASSERT_TRUE(buffer_node->IsValid()); |
| 32 | } |
| 33 | |
| 34 | void TearDown() override { |
| 35 | if (buffer_node != nullptr) { |
| 36 | delete buffer_node; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | BufferNode* buffer_node = nullptr; |
| 41 | }; |
| 42 | |
| 43 | TEST_F(BufferNodeTest, TestCreateBufferNode) { |
| 44 | EXPECT_EQ(buffer_node->user_metadata_size(), kUserMetadataSize); |
| 45 | // Test the handle just allocated is good (i.e. able to be imported) |
| 46 | GraphicBufferMapper& mapper = GraphicBufferMapper::get(); |
| 47 | const native_handle_t* outHandle; |
| 48 | status_t ret = |
| 49 | mapper.importBuffer(buffer_node->buffer_handle(), buffer_node->buffer_desc().width, |
| 50 | buffer_node->buffer_desc().height, |
| 51 | buffer_node->buffer_desc().layers, |
| 52 | buffer_node->buffer_desc().format, buffer_node->buffer_desc().usage, |
| 53 | buffer_node->buffer_desc().stride, &outHandle); |
| 54 | EXPECT_EQ(ret, OK); |
| 55 | EXPECT_THAT(outHandle, NotNull()); |
| 56 | } |
| 57 | |
| 58 | TEST_F(BufferNodeTest, TestAddNewActiveClientsBitToMask_twoNewClients) { |
Tianyu Jiang | a99f911 | 2018-12-13 18:23:07 -0800 | [diff] [blame^] | 59 | uint32_t new_client_state_mask_1 = buffer_node->AddNewActiveClientsBitToMask(); |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 60 | EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), new_client_state_mask_1); |
| 61 | |
| 62 | // Request and add a new client_state_mask again. |
| 63 | // Active clients bit mask should be the union of the two new |
| 64 | // client_state_masks. |
Tianyu Jiang | a99f911 | 2018-12-13 18:23:07 -0800 | [diff] [blame^] | 65 | uint32_t new_client_state_mask_2 = buffer_node->AddNewActiveClientsBitToMask(); |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 66 | EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), |
| 67 | new_client_state_mask_1 | new_client_state_mask_2); |
| 68 | } |
| 69 | |
| 70 | TEST_F(BufferNodeTest, TestAddNewActiveClientsBitToMask_32NewClients) { |
Tianyu Jiang | a99f911 | 2018-12-13 18:23:07 -0800 | [diff] [blame^] | 71 | uint32_t new_client_state_mask = 0U; |
| 72 | uint32_t current_mask = 0U; |
| 73 | uint32_t expected_mask = 0U; |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 74 | |
Fan Xu | d34a80a | 2018-12-04 11:32:39 -0800 | [diff] [blame] | 75 | for (int i = 0; i < BufferHubDefs::kMaxNumberOfClients; ++i) { |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 76 | new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); |
| 77 | EXPECT_NE(new_client_state_mask, 0); |
| 78 | EXPECT_FALSE(new_client_state_mask & current_mask); |
| 79 | expected_mask = current_mask | new_client_state_mask; |
| 80 | current_mask = buffer_node->GetActiveClientsBitMask(); |
| 81 | EXPECT_EQ(current_mask, expected_mask); |
| 82 | } |
| 83 | |
| 84 | // Method should fail upon requesting for more than maximum allowable clients. |
| 85 | new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); |
Tianyu Jiang | a99f911 | 2018-12-13 18:23:07 -0800 | [diff] [blame^] | 86 | EXPECT_EQ(new_client_state_mask, 0U); |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 87 | EXPECT_EQ(errno, E2BIG); |
| 88 | } |
| 89 | |
| 90 | TEST_F(BufferNodeTest, TestRemoveActiveClientsBitFromMask) { |
| 91 | buffer_node->AddNewActiveClientsBitToMask(); |
Tianyu Jiang | a99f911 | 2018-12-13 18:23:07 -0800 | [diff] [blame^] | 92 | uint32_t current_mask = buffer_node->GetActiveClientsBitMask(); |
| 93 | uint32_t new_client_state_mask = buffer_node->AddNewActiveClientsBitToMask(); |
Fan Xu | 574a685 | 2018-11-02 13:22:42 -0700 | [diff] [blame] | 94 | EXPECT_NE(buffer_node->GetActiveClientsBitMask(), current_mask); |
| 95 | |
| 96 | buffer_node->RemoveClientsBitFromMask(new_client_state_mask); |
| 97 | EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), current_mask); |
| 98 | |
| 99 | // Remove the test_mask again to the active client bit mask should not modify |
| 100 | // the value of active clients bit mask. |
| 101 | buffer_node->RemoveClientsBitFromMask(new_client_state_mask); |
| 102 | EXPECT_EQ(buffer_node->GetActiveClientsBitMask(), current_mask); |
| 103 | } |
| 104 | |
| 105 | } // namespace |
| 106 | |
| 107 | } // namespace implementation |
| 108 | } // namespace V1_0 |
| 109 | } // namespace bufferhub |
| 110 | } // namespace frameworks |
| 111 | } // namespace android |