1fa7767c5Sopenharmony_ci/* 2fa7767c5Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License. 5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at 6fa7767c5Sopenharmony_ci * 7fa7767c5Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fa7767c5Sopenharmony_ci * 9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and 13fa7767c5Sopenharmony_ci * limitations under the License. 14fa7767c5Sopenharmony_ci */ 15fa7767c5Sopenharmony_ci 16fa7767c5Sopenharmony_ci#include "avbuffer_unit_test.h" 17fa7767c5Sopenharmony_ci#include "av_hardware_allocator.h" 18fa7767c5Sopenharmony_ci#include "av_hardware_memory.h" 19fa7767c5Sopenharmony_ci#include "av_shared_allocator.h" 20fa7767c5Sopenharmony_ci#include "av_shared_memory_ext.h" 21fa7767c5Sopenharmony_ci#include "av_surface_allocator.h" 22fa7767c5Sopenharmony_ci#include "av_surface_memory.h" 23fa7767c5Sopenharmony_ci#include "avbuffer_mock.h" 24fa7767c5Sopenharmony_ci#include "avbuffer_utils.h" 25fa7767c5Sopenharmony_ci#include "unittest_log.h" 26fa7767c5Sopenharmony_ci 27fa7767c5Sopenharmony_ciusing namespace std; 28fa7767c5Sopenharmony_ciusing namespace testing::ext; 29fa7767c5Sopenharmony_ci 30fa7767c5Sopenharmony_cinamespace OHOS { 31fa7767c5Sopenharmony_cinamespace Media { 32fa7767c5Sopenharmony_cinamespace AVBufferUT { 33fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::SetUpTestCase(void) 34fa7767c5Sopenharmony_ci{ 35fa7767c5Sopenharmony_ci std::cout << "[SetUpTestCase]: SetUp!!!" << std::endl; 36fa7767c5Sopenharmony_ci} 37fa7767c5Sopenharmony_ci 38fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::TearDownTestCase(void) 39fa7767c5Sopenharmony_ci{ 40fa7767c5Sopenharmony_ci std::cout << "[TearDownTestCase]: over!!!" << std::endl; 41fa7767c5Sopenharmony_ci} 42fa7767c5Sopenharmony_ci 43fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::SetUp(void) 44fa7767c5Sopenharmony_ci{ 45fa7767c5Sopenharmony_ci std::cout << "[SetUp]: SetUp!!!"; 46fa7767c5Sopenharmony_ci meta_ = std::make_shared<Meta>(); 47fa7767c5Sopenharmony_ci const ::testing::TestInfo *testInfo_ = ::testing::UnitTest::GetInstance()->current_test_info(); 48fa7767c5Sopenharmony_ci std::string testName = testInfo_->name(); 49fa7767c5Sopenharmony_ci std::cout << testName << std::endl; 50fa7767c5Sopenharmony_ci parcel_ = nullptr; 51fa7767c5Sopenharmony_ci if (inputBuffer_.size() < TEST_BUFFER_SIZE) { 52fa7767c5Sopenharmony_ci inputBuffer_.resize(TEST_BUFFER_SIZE); 53fa7767c5Sopenharmony_ci for (int32_t i = 0; i < TEST_BUFFER_SIZE; ++i) { 54fa7767c5Sopenharmony_ci inputBuffer_[i] = i % 256; // 256: uint8_t range 55fa7767c5Sopenharmony_ci } 56fa7767c5Sopenharmony_ci } 57fa7767c5Sopenharmony_ci vector<uint8_t> temp(TEST_BUFFER_SIZE, 0); 58fa7767c5Sopenharmony_ci swap(temp, outputBuffer_); 59fa7767c5Sopenharmony_ci} 60fa7767c5Sopenharmony_ci 61fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::TearDown(void) 62fa7767c5Sopenharmony_ci{ 63fa7767c5Sopenharmony_ci allocator_ = nullptr; 64fa7767c5Sopenharmony_ci buffer_ = nullptr; 65fa7767c5Sopenharmony_ci meta_ = nullptr; 66fa7767c5Sopenharmony_ci parcel_ = nullptr; 67fa7767c5Sopenharmony_ci for (auto &buffer : dmaBufferLst_) { 68fa7767c5Sopenharmony_ci DmabufHeapBufferFree(&buffer); 69fa7767c5Sopenharmony_ci } 70fa7767c5Sopenharmony_ci std::vector<DmabufHeapBuffer> tmp; 71fa7767c5Sopenharmony_ci swap(tmp, dmaBufferLst_); 72fa7767c5Sopenharmony_ci std::cout << "[TearDown]: over!!!" << std::endl; 73fa7767c5Sopenharmony_ci} 74fa7767c5Sopenharmony_ci 75fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateLocalHardwareMem() 76fa7767c5Sopenharmony_ci{ 77fa7767c5Sopenharmony_ci // create loacal 78fa7767c5Sopenharmony_ci DmabufHeapBuffer dmaBuffer = {.size = capacity_, .heapFlags = 0}; 79fa7767c5Sopenharmony_ci int32_t dmaHeapFd = HardwareHeapFactory::GetInstance().GetHardwareHeapFd(); 80fa7767c5Sopenharmony_ci DmabufHeapBufferAlloc(dmaHeapFd, &dmaBuffer); 81fa7767c5Sopenharmony_ci dmaBufferLst_.push_back(dmaBuffer); 82fa7767c5Sopenharmony_ci 83fa7767c5Sopenharmony_ci allocator_ = AVAllocatorFactory::CreateHardwareAllocator(dmaBuffer.fd, capacity_, memFlag_); 84fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, allocator_); 85fa7767c5Sopenharmony_ci remoteBuffer_ = buffer_ = AVBuffer::CreateAVBuffer(allocator_, capacity_, align_); 86fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_); 87fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->memory_->GetAddr()); 88fa7767c5Sopenharmony_ci} 89fa7767c5Sopenharmony_ci 90fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateLocalHardwareMemSecure() 91fa7767c5Sopenharmony_ci{ 92fa7767c5Sopenharmony_ci // create loacal 93fa7767c5Sopenharmony_ci DmabufHeapBuffer dmaBuffer = {.size = capacity_, .heapFlags = 0}; 94fa7767c5Sopenharmony_ci int32_t dmaHeapFd = HardwareHeapFactory::GetInstance().GetHardwareHeapFd(); 95fa7767c5Sopenharmony_ci DmabufHeapBufferAlloc(dmaHeapFd, &dmaBuffer); 96fa7767c5Sopenharmony_ci dmaBufferLst_.push_back(dmaBuffer); 97fa7767c5Sopenharmony_ci 98fa7767c5Sopenharmony_ci allocator_ = AVAllocatorFactory::CreateHardwareAllocator(dmaBuffer.fd, capacity_, memFlag_, true); 99fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, allocator_); 100fa7767c5Sopenharmony_ci remoteBuffer_ = buffer_ = AVBuffer::CreateAVBuffer(allocator_, capacity_, align_); 101fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_); 102fa7767c5Sopenharmony_ci ASSERT_EQ(nullptr, buffer_->memory_->GetAddr()); 103fa7767c5Sopenharmony_ci} 104fa7767c5Sopenharmony_ci 105fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateLocalHardwareMemByConfig() 106fa7767c5Sopenharmony_ci{ 107fa7767c5Sopenharmony_ci // create loacal 108fa7767c5Sopenharmony_ci DmabufHeapBuffer dmaBuffer = {.size = capacity_, .heapFlags = 0}; 109fa7767c5Sopenharmony_ci int32_t dmaHeapFd = HardwareHeapFactory::GetInstance().GetHardwareHeapFd(); 110fa7767c5Sopenharmony_ci DmabufHeapBufferAlloc(dmaHeapFd, &dmaBuffer); 111fa7767c5Sopenharmony_ci dmaBufferLst_.push_back(dmaBuffer); 112fa7767c5Sopenharmony_ci 113fa7767c5Sopenharmony_ci config_.dmaFd = dmaBuffer.fd; 114fa7767c5Sopenharmony_ci config_.size = capacity_; 115fa7767c5Sopenharmony_ci config_.memoryFlag = memFlag_; 116fa7767c5Sopenharmony_ci config_.memoryType = MemoryType::HARDWARE_MEMORY; 117fa7767c5Sopenharmony_ci remoteBuffer_ = buffer_ = AVBuffer::CreateAVBuffer(config_); 118fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_); 119fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->memory_->GetAddr()); 120fa7767c5Sopenharmony_ci} 121fa7767c5Sopenharmony_ci 122fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateRemoteHardwareMem() 123fa7767c5Sopenharmony_ci{ 124fa7767c5Sopenharmony_ci parcel_ = std::make_shared<MessageParcel>(); 125fa7767c5Sopenharmony_ci // create remote 126fa7767c5Sopenharmony_ci CreateLocalHardwareMem(); 127fa7767c5Sopenharmony_ci buffer_ = nullptr; 128fa7767c5Sopenharmony_ci ASSERT_FALSE((allocator_ == nullptr) || (remoteBuffer_ == nullptr) || (remoteBuffer_->memory_ == nullptr)); 129fa7767c5Sopenharmony_ci std::cout << "remote fd: " << remoteBuffer_->memory_->GetFileDescriptor() << "\n"; 130fa7767c5Sopenharmony_ci} 131fa7767c5Sopenharmony_ci 132fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateLocalSharedMem() 133fa7767c5Sopenharmony_ci{ 134fa7767c5Sopenharmony_ci // create loacal 135fa7767c5Sopenharmony_ci allocator_ = AVAllocatorFactory::CreateSharedAllocator(memFlag_); 136fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, allocator_); 137fa7767c5Sopenharmony_ci remoteBuffer_ = buffer_ = AVBuffer::CreateAVBuffer(allocator_, capacity_, align_); 138fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_); 139fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->memory_->GetAddr()); 140fa7767c5Sopenharmony_ci} 141fa7767c5Sopenharmony_ci 142fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateLocalSharedMemByConfig() 143fa7767c5Sopenharmony_ci{ 144fa7767c5Sopenharmony_ci // create loacal 145fa7767c5Sopenharmony_ci config_.align = align_; 146fa7767c5Sopenharmony_ci config_.size = capacity_; 147fa7767c5Sopenharmony_ci config_.memoryFlag = memFlag_; 148fa7767c5Sopenharmony_ci config_.memoryType = MemoryType::SHARED_MEMORY; 149fa7767c5Sopenharmony_ci remoteBuffer_ = buffer_ = AVBuffer::CreateAVBuffer(config_); 150fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_); 151fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->memory_->GetAddr()); 152fa7767c5Sopenharmony_ci} 153fa7767c5Sopenharmony_ci 154fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateRemoteSharedMem() 155fa7767c5Sopenharmony_ci{ 156fa7767c5Sopenharmony_ci parcel_ = std::make_shared<MessageParcel>(); 157fa7767c5Sopenharmony_ci // create remote 158fa7767c5Sopenharmony_ci CreateLocalSharedMem(); 159fa7767c5Sopenharmony_ci buffer_ = nullptr; 160fa7767c5Sopenharmony_ci ASSERT_FALSE((allocator_ == nullptr) || (remoteBuffer_ == nullptr) || (remoteBuffer_->memory_ == nullptr)); 161fa7767c5Sopenharmony_ci std::cout << "remote fd: " << remoteBuffer_->memory_->GetFileDescriptor() << "\n"; 162fa7767c5Sopenharmony_ci} 163fa7767c5Sopenharmony_ci 164fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateLocalSurfaceMem() 165fa7767c5Sopenharmony_ci{ 166fa7767c5Sopenharmony_ci // create loacal 167fa7767c5Sopenharmony_ci allocator_ = AVAllocatorFactory::CreateSurfaceAllocator(DEFAULT_CONFIG); 168fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, allocator_); 169fa7767c5Sopenharmony_ci remoteBuffer_ = buffer_ = AVBuffer::CreateAVBuffer(allocator_, capacity_, align_); 170fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_); 171fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->memory_->GetAddr()); 172fa7767c5Sopenharmony_ci} 173fa7767c5Sopenharmony_ci 174fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateLocalSurfaceMemByConfig() 175fa7767c5Sopenharmony_ci{ 176fa7767c5Sopenharmony_ci // create loacal 177fa7767c5Sopenharmony_ci *(config_.surfaceBufferConfig) = DEFAULT_CONFIG; 178fa7767c5Sopenharmony_ci config_.memoryType = MemoryType::SURFACE_MEMORY; 179fa7767c5Sopenharmony_ci remoteBuffer_ = buffer_ = AVBuffer::CreateAVBuffer(config_); 180fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_); 181fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->memory_->GetAddr()); 182fa7767c5Sopenharmony_ci} 183fa7767c5Sopenharmony_ci 184fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateRemoteSurfaceMem() 185fa7767c5Sopenharmony_ci{ 186fa7767c5Sopenharmony_ci parcel_ = std::make_shared<MessageParcel>(); 187fa7767c5Sopenharmony_ci // create remote 188fa7767c5Sopenharmony_ci CreateLocalSurfaceMem(); 189fa7767c5Sopenharmony_ci buffer_ = nullptr; 190fa7767c5Sopenharmony_ci ASSERT_FALSE((allocator_ == nullptr) || (remoteBuffer_ == nullptr) || (remoteBuffer_->memory_ == nullptr)); 191fa7767c5Sopenharmony_ci std::cout << "remote fd: " << remoteBuffer_->memory_->GetFileDescriptor() << "\n"; 192fa7767c5Sopenharmony_ci} 193fa7767c5Sopenharmony_ci 194fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateLocalSurfaceMemByParcel() 195fa7767c5Sopenharmony_ci{ 196fa7767c5Sopenharmony_ci MessageParcel parcel; 197fa7767c5Sopenharmony_ci sptr<SurfaceBuffer> surfaceBuffer = SurfaceBuffer::Create(); 198fa7767c5Sopenharmony_ci (void)surfaceBuffer->Alloc(DEFAULT_CONFIG); 199fa7767c5Sopenharmony_ci (void)surfaceBuffer->WriteToMessageParcel(parcel); 200fa7767c5Sopenharmony_ci // create local 201fa7767c5Sopenharmony_ci remoteBuffer_ = buffer_ = AVBuffer::CreateAVBuffer(); 202fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_); 203fa7767c5Sopenharmony_ci ASSERT_TRUE(buffer_->ReadFromMessageParcel(parcel, true)); 204fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->memory_->GetAddr()); 205fa7767c5Sopenharmony_ci} 206fa7767c5Sopenharmony_ci 207fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateLocalSurfaceMemBySptr() 208fa7767c5Sopenharmony_ci{ 209fa7767c5Sopenharmony_ci sptr<SurfaceBuffer> surfaceBuffer = SurfaceBuffer::Create(); 210fa7767c5Sopenharmony_ci (void)surfaceBuffer->Alloc(DEFAULT_CONFIG); 211fa7767c5Sopenharmony_ci // create local 212fa7767c5Sopenharmony_ci remoteBuffer_ = buffer_ = AVBuffer::CreateAVBuffer(surfaceBuffer); 213fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_); 214fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->memory_); 215fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->memory_->GetAddr()); 216fa7767c5Sopenharmony_ci} 217fa7767c5Sopenharmony_ci 218fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateRemoteSurfaceMemByParcel() 219fa7767c5Sopenharmony_ci{ 220fa7767c5Sopenharmony_ci parcel_ = std::make_shared<MessageParcel>(); 221fa7767c5Sopenharmony_ci // create remote 222fa7767c5Sopenharmony_ci CreateLocalSurfaceMemByParcel(); 223fa7767c5Sopenharmony_ci buffer_ = nullptr; 224fa7767c5Sopenharmony_ci ASSERT_FALSE((remoteBuffer_ == nullptr) || (remoteBuffer_->memory_ == nullptr)); 225fa7767c5Sopenharmony_ci std::cout << "remote fd: " << remoteBuffer_->memory_->GetFileDescriptor() << "\n"; 226fa7767c5Sopenharmony_ci} 227fa7767c5Sopenharmony_ci 228fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateRemoteSurfaceMemBySptr() 229fa7767c5Sopenharmony_ci{ 230fa7767c5Sopenharmony_ci parcel_ = std::make_shared<MessageParcel>(); 231fa7767c5Sopenharmony_ci // create remote 232fa7767c5Sopenharmony_ci CreateLocalSurfaceMemBySptr(); 233fa7767c5Sopenharmony_ci buffer_ = nullptr; 234fa7767c5Sopenharmony_ci ASSERT_FALSE((remoteBuffer_ == nullptr) || (remoteBuffer_->memory_ == nullptr)); 235fa7767c5Sopenharmony_ci std::cout << "remote fd: " << remoteBuffer_->memory_->GetFileDescriptor() << "\n"; 236fa7767c5Sopenharmony_ci} 237fa7767c5Sopenharmony_ci 238fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateLocalVirtualMem() 239fa7767c5Sopenharmony_ci{ 240fa7767c5Sopenharmony_ci // create loacal 241fa7767c5Sopenharmony_ci allocator_ = AVAllocatorFactory::CreateVirtualAllocator(); 242fa7767c5Sopenharmony_ci remoteBuffer_ = buffer_ = AVBuffer::CreateAVBuffer(allocator_, capacity_, align_); 243fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_); 244fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->memory_->GetAddr()); 245fa7767c5Sopenharmony_ci} 246fa7767c5Sopenharmony_ci 247fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateLocalVirtualMemByConfig() 248fa7767c5Sopenharmony_ci{ 249fa7767c5Sopenharmony_ci // create loacal 250fa7767c5Sopenharmony_ci config_.align = align_; 251fa7767c5Sopenharmony_ci config_.size = capacity_; 252fa7767c5Sopenharmony_ci config_.memoryType = MemoryType::VIRTUAL_MEMORY; 253fa7767c5Sopenharmony_ci remoteBuffer_ = buffer_ = AVBuffer::CreateAVBuffer(config_); 254fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_); 255fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->memory_->GetAddr()); 256fa7767c5Sopenharmony_ci} 257fa7767c5Sopenharmony_ci 258fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CreateLocalNullMem() 259fa7767c5Sopenharmony_ci{ 260fa7767c5Sopenharmony_ci parcel_ = std::make_shared<MessageParcel>(); 261fa7767c5Sopenharmony_ci // create remote 262fa7767c5Sopenharmony_ci remoteBuffer_ = AVBuffer::CreateAVBuffer(); 263fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, remoteBuffer_); 264fa7767c5Sopenharmony_ci} 265fa7767c5Sopenharmony_ci 266fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::GetRemoteBuffer() 267fa7767c5Sopenharmony_ci{ 268fa7767c5Sopenharmony_ci bool ret = remoteBuffer_->WriteToMessageParcel(*parcel_); 269fa7767c5Sopenharmony_ci ASSERT_TRUE(ret); 270fa7767c5Sopenharmony_ci // create loacal 271fa7767c5Sopenharmony_ci buffer_ = AVBuffer::CreateAVBuffer(); 272fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_); 273fa7767c5Sopenharmony_ci 274fa7767c5Sopenharmony_ci ret = buffer_->ReadFromMessageParcel(*parcel_); 275fa7767c5Sopenharmony_ci ASSERT_TRUE(ret); 276fa7767c5Sopenharmony_ci if (buffer_->memory_ != nullptr) { 277fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->memory_->GetAddr()); 278fa7767c5Sopenharmony_ci std::cout << "local fd: " << buffer_->memory_->GetFileDescriptor() << "\n"; 279fa7767c5Sopenharmony_ci } 280fa7767c5Sopenharmony_ci} 281fa7767c5Sopenharmony_ci 282fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CheckMetaTransParcel() 283fa7767c5Sopenharmony_ci{ 284fa7767c5Sopenharmony_ci int32_t getIntValue = 0; 285fa7767c5Sopenharmony_ci int64_t getLongValue = 0; 286fa7767c5Sopenharmony_ci double getDoubleValue = 0.0; 287fa7767c5Sopenharmony_ci std::string getStringValue = ""; 288fa7767c5Sopenharmony_ci MessageParcel parcel; 289fa7767c5Sopenharmony_ci for (int32_t toIndex = 0; toIndex < TEST_LOOP_DEPTH; ++toIndex) { 290fa7767c5Sopenharmony_ci ASSERT_TRUE(meta_->ToParcel(parcel)); 291fa7767c5Sopenharmony_ci } 292fa7767c5Sopenharmony_ci for (int32_t fromIndex = 0; fromIndex < TEST_LOOP_DEPTH; ++fromIndex) { 293fa7767c5Sopenharmony_ci ASSERT_TRUE(meta_->FromParcel(parcel)); 294fa7767c5Sopenharmony_ci meta_->GetData(INT_TESTKEY, getIntValue); 295fa7767c5Sopenharmony_ci meta_->GetData(LONG_TESTKEY, getLongValue); 296fa7767c5Sopenharmony_ci meta_->GetData(DOUBLE_TESTKEY, getDoubleValue); 297fa7767c5Sopenharmony_ci meta_->GetData(STRING_TESTKEY, getStringValue); 298fa7767c5Sopenharmony_ci 299fa7767c5Sopenharmony_ci EXPECT_EQ(getIntValue, INTVALUE); 300fa7767c5Sopenharmony_ci EXPECT_EQ(getLongValue, LONGVALUE); 301fa7767c5Sopenharmony_ci EXPECT_EQ(getDoubleValue, DOUBLEVALUE); 302fa7767c5Sopenharmony_ci EXPECT_EQ(getStringValue, STRINGVALUE); 303fa7767c5Sopenharmony_ci } 304fa7767c5Sopenharmony_ci} 305fa7767c5Sopenharmony_ci 306fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CheckMetaSetAndGet() 307fa7767c5Sopenharmony_ci{ 308fa7767c5Sopenharmony_ci int32_t getIntValue = 0; 309fa7767c5Sopenharmony_ci int64_t getLongValue = 0; 310fa7767c5Sopenharmony_ci double getDoubleValue = 0.0; 311fa7767c5Sopenharmony_ci std::string getStringValue = ""; 312fa7767c5Sopenharmony_ci 313fa7767c5Sopenharmony_ci remoteBuffer_->pts_ = DEFAULT_PTS; 314fa7767c5Sopenharmony_ci remoteBuffer_->dts_ = DEFAULT_DTS; 315fa7767c5Sopenharmony_ci remoteBuffer_->duration_ = DEFAULT_DURATION; 316fa7767c5Sopenharmony_ci remoteBuffer_->flag_ = DEFAULT_FLAG; 317fa7767c5Sopenharmony_ci 318fa7767c5Sopenharmony_ci meta_->SetData(INT_TESTKEY, INTVALUE); 319fa7767c5Sopenharmony_ci meta_->SetData(LONG_TESTKEY, LONGVALUE); 320fa7767c5Sopenharmony_ci meta_->SetData(DOUBLE_TESTKEY, DOUBLEVALUE); 321fa7767c5Sopenharmony_ci meta_->SetData(STRING_TESTKEY, STRINGVALUE); 322fa7767c5Sopenharmony_ci CheckMetaTransParcel(); 323fa7767c5Sopenharmony_ci remoteBuffer_->meta_ = meta_; 324fa7767c5Sopenharmony_ci 325fa7767c5Sopenharmony_ci if (parcel_ != nullptr) { 326fa7767c5Sopenharmony_ci GetRemoteBuffer(); 327fa7767c5Sopenharmony_ci ASSERT_EQ(remoteBuffer_->GetUniqueId(), buffer_->GetUniqueId()); 328fa7767c5Sopenharmony_ci ASSERT_FALSE((buffer_ == nullptr) || (buffer_->memory_ == nullptr)); 329fa7767c5Sopenharmony_ci } 330fa7767c5Sopenharmony_ci ASSERT_NE(nullptr, buffer_->meta_); 331fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->pts_, DEFAULT_PTS); 332fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->dts_, DEFAULT_DTS); 333fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->duration_, DEFAULT_DURATION); 334fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->flag_, DEFAULT_FLAG); 335fa7767c5Sopenharmony_ci 336fa7767c5Sopenharmony_ci buffer_->meta_->GetData(INT_TESTKEY, getIntValue); 337fa7767c5Sopenharmony_ci buffer_->meta_->GetData(LONG_TESTKEY, getLongValue); 338fa7767c5Sopenharmony_ci buffer_->meta_->GetData(DOUBLE_TESTKEY, getDoubleValue); 339fa7767c5Sopenharmony_ci buffer_->meta_->GetData(STRING_TESTKEY, getStringValue); 340fa7767c5Sopenharmony_ci 341fa7767c5Sopenharmony_ci EXPECT_EQ(getIntValue, INTVALUE); 342fa7767c5Sopenharmony_ci EXPECT_EQ(getLongValue, LONGVALUE); 343fa7767c5Sopenharmony_ci EXPECT_EQ(getDoubleValue, DOUBLEVALUE); 344fa7767c5Sopenharmony_ci EXPECT_EQ(getStringValue, STRINGVALUE); 345fa7767c5Sopenharmony_ci} 346fa7767c5Sopenharmony_ci 347fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CheckAttrTrans() 348fa7767c5Sopenharmony_ci{ 349fa7767c5Sopenharmony_ci remoteBuffer_->pts_ = DEFAULT_PTS; 350fa7767c5Sopenharmony_ci remoteBuffer_->dts_ = DEFAULT_DTS; 351fa7767c5Sopenharmony_ci remoteBuffer_->duration_ = DEFAULT_DURATION; 352fa7767c5Sopenharmony_ci remoteBuffer_->flag_ = DEFAULT_FLAG; 353fa7767c5Sopenharmony_ci 354fa7767c5Sopenharmony_ci GetRemoteBuffer(); 355fa7767c5Sopenharmony_ci ASSERT_EQ(remoteBuffer_->GetUniqueId(), buffer_->GetUniqueId()); 356fa7767c5Sopenharmony_ci ASSERT_FALSE(buffer_ == nullptr); 357fa7767c5Sopenharmony_ci 358fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->pts_, DEFAULT_PTS); 359fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->dts_, DEFAULT_DTS); 360fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->duration_, DEFAULT_DURATION); 361fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->flag_, DEFAULT_FLAG); 362fa7767c5Sopenharmony_ci 363fa7767c5Sopenharmony_ci for (int32_t i = 0; i < TEST_LOOP_DEPTH; i++) { 364fa7767c5Sopenharmony_ci remoteBuffer_->pts_++; 365fa7767c5Sopenharmony_ci remoteBuffer_->dts_++; 366fa7767c5Sopenharmony_ci remoteBuffer_->duration_++; 367fa7767c5Sopenharmony_ci remoteBuffer_->flag_++; 368fa7767c5Sopenharmony_ci 369fa7767c5Sopenharmony_ci ASSERT_TRUE(remoteBuffer_->WriteToMessageParcel(*parcel_)); 370fa7767c5Sopenharmony_ci ASSERT_TRUE(remoteBuffer_->WriteToMessageParcel(*parcel_)); 371fa7767c5Sopenharmony_ci ASSERT_TRUE(buffer_->ReadFromMessageParcel(*parcel_)); 372fa7767c5Sopenharmony_ci ASSERT_TRUE(buffer_->ReadFromMessageParcel(*parcel_)); 373fa7767c5Sopenharmony_ci 374fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->pts_, remoteBuffer_->pts_); 375fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->dts_, remoteBuffer_->dts_); 376fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->duration_, remoteBuffer_->duration_); 377fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->flag_, remoteBuffer_->flag_); 378fa7767c5Sopenharmony_ci } 379fa7767c5Sopenharmony_ci} 380fa7767c5Sopenharmony_ci 381fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CheckMemTrans() 382fa7767c5Sopenharmony_ci{ 383fa7767c5Sopenharmony_ci outputBuffer_.resize(TEST_BUFFER_SIZE, 0); 384fa7767c5Sopenharmony_ci int32_t pos = capacity_ / 2; 385fa7767c5Sopenharmony_ci int32_t length = capacity_ - pos; 386fa7767c5Sopenharmony_ci auto error = memcpy_s(remoteBuffer_->memory_->GetAddr(), pos, inputBuffer_.data(), pos); 387fa7767c5Sopenharmony_ci EXPECT_EQ(error, EOK); 388fa7767c5Sopenharmony_ci remoteBuffer_->memory_->SetSize(pos); 389fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->Write(inputBuffer_.data() + pos, length, -1), length); 390fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->Read(outputBuffer_.data(), capacity_, 0), capacity_); 391fa7767c5Sopenharmony_ci uint8_t *addr = remoteBuffer_->memory_->GetAddr(); 392fa7767c5Sopenharmony_ci ASSERT_NE(addr, nullptr); 393fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->GetSize(), capacity_); 394fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(inputBuffer_.data(), outputBuffer_.data(), capacity_), 0); 395fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(static_cast<void *>(addr), inputBuffer_.data(), capacity_), 0); 396fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(static_cast<void *>(addr), outputBuffer_.data(), capacity_), 0); 397fa7767c5Sopenharmony_ci if (parcel_ != nullptr) { 398fa7767c5Sopenharmony_ci GetRemoteBuffer(); 399fa7767c5Sopenharmony_ci ASSERT_EQ(remoteBuffer_->GetUniqueId(), buffer_->GetUniqueId()); 400fa7767c5Sopenharmony_ci ASSERT_FALSE((buffer_ == nullptr) || (buffer_->memory_ == nullptr)); 401fa7767c5Sopenharmony_ci } 402fa7767c5Sopenharmony_ci ASSERT_EQ(buffer_->memory_->GetSize(), capacity_); 403fa7767c5Sopenharmony_ci addr = buffer_->memory_->GetAddr(); 404fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->memory_->Read(outputBuffer_.data(), capacity_, 0), capacity_); 405fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(inputBuffer_.data(), outputBuffer_.data(), capacity_), 0); 406fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(static_cast<void *>(addr), inputBuffer_.data(), capacity_), 0); 407fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(static_cast<void *>(addr), outputBuffer_.data(), capacity_), 0); 408fa7767c5Sopenharmony_ci} 409fa7767c5Sopenharmony_ci 410fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CheckMemTransPos(int32_t pos) 411fa7767c5Sopenharmony_ci{ 412fa7767c5Sopenharmony_ci outputBuffer_.resize(TEST_BUFFER_SIZE, 0); 413fa7767c5Sopenharmony_ci capacity_ = remoteBuffer_->memory_->GetCapacity(); 414fa7767c5Sopenharmony_ci int32_t length = capacity_ - pos; 415fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->Write(inputBuffer_.data() + pos, length, pos), length); 416fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->Read(outputBuffer_.data() + pos, length, pos), length); 417fa7767c5Sopenharmony_ci uint8_t *addr = remoteBuffer_->memory_->GetAddr(); 418fa7767c5Sopenharmony_ci ASSERT_NE(addr, nullptr); 419fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->GetSize(), capacity_); 420fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(inputBuffer_.data() + pos, outputBuffer_.data() + pos, length), 0); 421fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(static_cast<void *>(addr + pos), inputBuffer_.data() + pos, length), 0); 422fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(static_cast<void *>(addr + pos), outputBuffer_.data() + pos, length), 0); 423fa7767c5Sopenharmony_ci if (parcel_ != nullptr) { 424fa7767c5Sopenharmony_ci GetRemoteBuffer(); 425fa7767c5Sopenharmony_ci ASSERT_EQ(remoteBuffer_->GetUniqueId(), buffer_->GetUniqueId()); 426fa7767c5Sopenharmony_ci ASSERT_FALSE((buffer_ == nullptr) || (buffer_->memory_ == nullptr)); 427fa7767c5Sopenharmony_ci } 428fa7767c5Sopenharmony_ci ASSERT_EQ(buffer_->memory_->GetSize(), capacity_); 429fa7767c5Sopenharmony_ci addr = buffer_->memory_->GetAddr(); 430fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->memory_->Read(outputBuffer_.data() + pos, length, pos), length); 431fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(inputBuffer_.data() + pos, outputBuffer_.data() + pos, length), 0); 432fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(static_cast<void *>(addr + pos), inputBuffer_.data() + pos, length), 0); 433fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(static_cast<void *>(addr + pos), outputBuffer_.data() + pos, length), 0); 434fa7767c5Sopenharmony_ci} 435fa7767c5Sopenharmony_ci 436fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CheckMemTransOutOfRange(int32_t pos) 437fa7767c5Sopenharmony_ci{ 438fa7767c5Sopenharmony_ci outputBuffer_.resize(TEST_BUFFER_SIZE, 0); 439fa7767c5Sopenharmony_ci capacity_ = remoteBuffer_->memory_->GetCapacity(); 440fa7767c5Sopenharmony_ci int32_t length = capacity_ - pos; 441fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->Write(inputBuffer_.data() + pos, length + 1, pos), length); 442fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->Read(outputBuffer_.data() + pos, length + 1, pos), length); 443fa7767c5Sopenharmony_ci uint8_t *addr = remoteBuffer_->memory_->GetAddr(); 444fa7767c5Sopenharmony_ci ASSERT_NE(addr, nullptr); 445fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->GetSize(), capacity_); 446fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(inputBuffer_.data() + pos, outputBuffer_.data() + pos, length), 0); 447fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(static_cast<void *>(addr + pos), inputBuffer_.data() + pos, length), 0); 448fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(static_cast<void *>(addr + pos), outputBuffer_.data() + pos, length), 0); 449fa7767c5Sopenharmony_ci if (parcel_ != nullptr) { 450fa7767c5Sopenharmony_ci GetRemoteBuffer(); 451fa7767c5Sopenharmony_ci ASSERT_EQ(remoteBuffer_->GetUniqueId(), buffer_->GetUniqueId()); 452fa7767c5Sopenharmony_ci ASSERT_FALSE((buffer_ == nullptr) || (buffer_->memory_ == nullptr)); 453fa7767c5Sopenharmony_ci } 454fa7767c5Sopenharmony_ci ASSERT_EQ(buffer_->memory_->GetSize(), capacity_); 455fa7767c5Sopenharmony_ci addr = buffer_->memory_->GetAddr(); 456fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->memory_->Read(outputBuffer_.data() + pos, length + 1, pos), length); 457fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(inputBuffer_.data() + pos, outputBuffer_.data() + pos, length), 0); 458fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(static_cast<void *>(addr + pos), inputBuffer_.data() + pos, length), 0); 459fa7767c5Sopenharmony_ci EXPECT_EQ(memcmp(static_cast<void *>(addr + pos), outputBuffer_.data() + pos, length), 0); 460fa7767c5Sopenharmony_ci} 461fa7767c5Sopenharmony_ci 462fa7767c5Sopenharmony_civoid AVBufferInnerUnitTest::CheckDataSize() 463fa7767c5Sopenharmony_ci{ 464fa7767c5Sopenharmony_ci capacity_ = remoteBuffer_->memory_->GetCapacity(); 465fa7767c5Sopenharmony_ci EXPECT_EQ(Status::OK, remoteBuffer_->memory_->SetSize(capacity_ - 1)); 466fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->GetSize(), capacity_ - 1); 467fa7767c5Sopenharmony_ci EXPECT_EQ(Status::OK, remoteBuffer_->memory_->SetSize(0)); 468fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->GetSize(), 0); 469fa7767c5Sopenharmony_ci EXPECT_EQ(Status::OK, remoteBuffer_->memory_->SetSize(1)); 470fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->GetSize(), 1); 471fa7767c5Sopenharmony_ci EXPECT_EQ(Status::ERROR_INVALID_PARAMETER, remoteBuffer_->memory_->SetSize(-1)); 472fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->GetSize(), 1); 473fa7767c5Sopenharmony_ci EXPECT_EQ(Status::ERROR_INVALID_PARAMETER, remoteBuffer_->memory_->SetSize(capacity_ + 1)); 474fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->GetSize(), 1); 475fa7767c5Sopenharmony_ci EXPECT_EQ(Status::OK, remoteBuffer_->memory_->SetSize(capacity_)); 476fa7767c5Sopenharmony_ci EXPECT_EQ(remoteBuffer_->memory_->GetSize(), capacity_); 477fa7767c5Sopenharmony_ci if (parcel_ != nullptr) { 478fa7767c5Sopenharmony_ci GetRemoteBuffer(); 479fa7767c5Sopenharmony_ci ASSERT_EQ(remoteBuffer_->GetUniqueId(), buffer_->GetUniqueId()); 480fa7767c5Sopenharmony_ci ASSERT_FALSE((buffer_ == nullptr) || (buffer_->memory_ == nullptr)); 481fa7767c5Sopenharmony_ci } 482fa7767c5Sopenharmony_ci EXPECT_EQ(buffer_->memory_->GetSize(), capacity_); 483fa7767c5Sopenharmony_ci} 484fa7767c5Sopenharmony_ci 485fa7767c5Sopenharmony_ciHardwareHeapFactory::HardwareHeapFactory() 486fa7767c5Sopenharmony_ci{ 487fa7767c5Sopenharmony_ci std::string rootDir = "/dev/dma_heap/"; 488fa7767c5Sopenharmony_ci DIR *dir = opendir(rootDir.c_str()); 489fa7767c5Sopenharmony_ci if (dir == nullptr) { 490fa7767c5Sopenharmony_ci return; 491fa7767c5Sopenharmony_ci } 492fa7767c5Sopenharmony_ci struct dirent *ptr; 493fa7767c5Sopenharmony_ci std::string heapName = ""; 494fa7767c5Sopenharmony_ci while ((ptr = readdir(dir)) != nullptr) { 495fa7767c5Sopenharmony_ci std::string fileName = ptr->d_name; 496fa7767c5Sopenharmony_ci std::string::size_type idx = fileName.find("system"); 497fa7767c5Sopenharmony_ci if (idx != std::string::npos) { 498fa7767c5Sopenharmony_ci heapName = fileName; 499fa7767c5Sopenharmony_ci break; 500fa7767c5Sopenharmony_ci } 501fa7767c5Sopenharmony_ci } 502fa7767c5Sopenharmony_ci closedir(dir); 503fa7767c5Sopenharmony_ci dmaHeapFd_ = DmabufHeapOpen(heapName.c_str()); 504fa7767c5Sopenharmony_ci} 505fa7767c5Sopenharmony_ci 506fa7767c5Sopenharmony_ciHardwareHeapFactory::~HardwareHeapFactory() 507fa7767c5Sopenharmony_ci{ 508fa7767c5Sopenharmony_ci DmabufHeapClose(dmaHeapFd_); 509fa7767c5Sopenharmony_ci} 510fa7767c5Sopenharmony_ci 511fa7767c5Sopenharmony_ciHardwareHeapFactory &HardwareHeapFactory::GetInstance() 512fa7767c5Sopenharmony_ci{ 513fa7767c5Sopenharmony_ci static HardwareHeapFactory hwHeapInstance; 514fa7767c5Sopenharmony_ci return hwHeapInstance; 515fa7767c5Sopenharmony_ci} 516fa7767c5Sopenharmony_ci 517fa7767c5Sopenharmony_ciint32_t HardwareHeapFactory::GetHardwareHeapFd() const 518fa7767c5Sopenharmony_ci{ 519fa7767c5Sopenharmony_ci return dmaHeapFd_; 520fa7767c5Sopenharmony_ci} 521fa7767c5Sopenharmony_ci 522fa7767c5Sopenharmony_civoid AVBufferFrameworkUnitTest::SetUpTestCase(void) {} 523fa7767c5Sopenharmony_ci 524fa7767c5Sopenharmony_civoid AVBufferFrameworkUnitTest::TearDownTestCase(void) {} 525fa7767c5Sopenharmony_ci 526fa7767c5Sopenharmony_civoid AVBufferFrameworkUnitTest::SetUp(void) 527fa7767c5Sopenharmony_ci{ 528fa7767c5Sopenharmony_ci std::cout << "[SetUp]: SetUp!!!" << std::endl; 529fa7767c5Sopenharmony_ci const ::testing::TestInfo *testInfo_ = ::testing::UnitTest::GetInstance()->current_test_info(); 530fa7767c5Sopenharmony_ci std::string testName = testInfo_->name(); 531fa7767c5Sopenharmony_ci std::cout << testName << std::endl; 532fa7767c5Sopenharmony_ci 533fa7767c5Sopenharmony_ci buffer_ = AVBufferMockFactory::CreateAVBuffer(MEMSIZE); 534fa7767c5Sopenharmony_ci EXPECT_NE(nullptr, buffer_); 535fa7767c5Sopenharmony_ci} 536fa7767c5Sopenharmony_ci 537fa7767c5Sopenharmony_civoid AVBufferFrameworkUnitTest::TearDown(void) 538fa7767c5Sopenharmony_ci{ 539fa7767c5Sopenharmony_ci std::cout << "[TearDown]: over!!!" << std::endl; 540fa7767c5Sopenharmony_ci} 541fa7767c5Sopenharmony_ci} // namespace AVBufferUT 542fa7767c5Sopenharmony_ci} // namespace Media 543fa7767c5Sopenharmony_ci} // namespace OHOS