Jiwen 'Steve' Cai | 4e566ed | 2018-09-19 22:41:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
Jiwen 'Steve' Cai | 8f51ec6 | 2018-08-07 21:50:51 -0700 | [diff] [blame] | 18 | #include <ui/BufferHubMetadata.h> |
Jiwen 'Steve' Cai | 4e566ed | 2018-09-19 22:41:42 -0700 | [diff] [blame] | 19 | |
| 20 | using android::dvr::BufferHubDefs::IsBufferGained; |
| 21 | |
| 22 | namespace android { |
| 23 | namespace dvr { |
| 24 | |
| 25 | constexpr size_t kEmptyUserMetadataSize = 0; |
| 26 | |
| 27 | class BufferHubMetadataTest : public ::testing::Test {}; |
| 28 | |
| 29 | TEST_F(BufferHubMetadataTest, Create_UserMetdataSizeTooBig) { |
| 30 | BufferHubMetadata m1 = |
| 31 | BufferHubMetadata::Create(std::numeric_limits<uint32_t>::max()); |
| 32 | EXPECT_FALSE(m1.IsValid()); |
| 33 | } |
| 34 | |
| 35 | TEST_F(BufferHubMetadataTest, Create_Success) { |
| 36 | BufferHubMetadata m1 = BufferHubMetadata::Create(kEmptyUserMetadataSize); |
| 37 | EXPECT_TRUE(m1.IsValid()); |
| 38 | EXPECT_NE(m1.metadata_header(), nullptr); |
| 39 | } |
| 40 | |
| 41 | TEST_F(BufferHubMetadataTest, Import_Success) { |
| 42 | BufferHubMetadata m1 = BufferHubMetadata::Create(kEmptyUserMetadataSize); |
| 43 | EXPECT_TRUE(m1.IsValid()); |
| 44 | EXPECT_NE(m1.metadata_header(), nullptr); |
| 45 | |
| 46 | pdx::LocalHandle h2 = m1.ashmem_handle().Duplicate(); |
| 47 | EXPECT_TRUE(h2.IsValid()); |
| 48 | |
| 49 | BufferHubMetadata m2 = BufferHubMetadata::Import(std::move(h2)); |
| 50 | EXPECT_FALSE(h2.IsValid()); |
| 51 | EXPECT_TRUE(m1.IsValid()); |
| 52 | BufferHubDefs::MetadataHeader* mh1 = m1.metadata_header(); |
| 53 | EXPECT_NE(mh1, nullptr); |
| 54 | |
| 55 | // TODO(b/111976433): Update this test once BufferHub state machine gets |
| 56 | // updated. In the old model, buffer starts in the gained state (i.e. |
| 57 | // valued 0). In the new model, buffer states in the released state. |
| 58 | EXPECT_TRUE(IsBufferGained(mh1->fence_state.load())); |
| 59 | |
| 60 | EXPECT_TRUE(m2.IsValid()); |
| 61 | BufferHubDefs::MetadataHeader* mh2 = m2.metadata_header(); |
| 62 | EXPECT_NE(mh2, nullptr); |
| 63 | |
| 64 | // TODO(b/111976433): Update this test once BufferHub state machine gets |
| 65 | // updated. In the old model, buffer starts in the gained state (i.e. |
| 66 | // valued 0). In the new model, buffer states in the released state. |
| 67 | EXPECT_TRUE(IsBufferGained(mh2->fence_state.load())); |
| 68 | } |
| 69 | |
| 70 | TEST_F(BufferHubMetadataTest, MoveMetadataInvalidatesOldOne) { |
| 71 | BufferHubMetadata m1 = BufferHubMetadata::Create(sizeof(int)); |
| 72 | EXPECT_TRUE(m1.IsValid()); |
| 73 | EXPECT_NE(m1.metadata_header(), nullptr); |
| 74 | EXPECT_TRUE(m1.ashmem_handle().IsValid()); |
| 75 | EXPECT_EQ(m1.user_metadata_size(), sizeof(int)); |
| 76 | |
| 77 | BufferHubMetadata m2 = std::move(m1); |
| 78 | |
| 79 | // After the move, the metadata header (a raw pointer) should be reset in the |
| 80 | // older buffer. |
| 81 | EXPECT_EQ(m1.metadata_header(), nullptr); |
| 82 | EXPECT_NE(m2.metadata_header(), nullptr); |
| 83 | |
| 84 | EXPECT_FALSE(m1.ashmem_handle().IsValid()); |
| 85 | EXPECT_TRUE(m2.ashmem_handle().IsValid()); |
| 86 | |
| 87 | EXPECT_EQ(m1.user_metadata_size(), 0U); |
| 88 | EXPECT_EQ(m2.user_metadata_size(), sizeof(int)); |
| 89 | |
| 90 | BufferHubMetadata m3{std::move(m2)}; |
| 91 | |
| 92 | // After the move, the metadata header (a raw pointer) should be reset in the |
| 93 | // older buffer. |
| 94 | EXPECT_EQ(m2.metadata_header(), nullptr); |
| 95 | EXPECT_NE(m3.metadata_header(), nullptr); |
| 96 | |
| 97 | EXPECT_FALSE(m2.ashmem_handle().IsValid()); |
| 98 | EXPECT_TRUE(m3.ashmem_handle().IsValid()); |
| 99 | |
| 100 | EXPECT_EQ(m2.user_metadata_size(), 0U); |
| 101 | EXPECT_EQ(m3.user_metadata_size(), sizeof(int)); |
| 102 | } |
| 103 | |
| 104 | } // namespace dvr |
| 105 | } // namespace android |