• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
17 
18 #include "share_memory.h"
19 #include "inner_api/common/log.h"
20 #include "cpp_ext/type_cast_ext.h"
21 #include "securec.h"
22 
23 namespace OHOS {
24 namespace Media {
25 namespace Plugins {
ShareMemory(size_t capacity,std::shared_ptr<Allocator> allocator,size_t align)26 ShareMemory::ShareMemory(size_t capacity, std::shared_ptr<Allocator> allocator, size_t align)
27     : Memory(capacity, std::move(allocator), align, MemoryType::SHARED_MEMORY, false)
28 {
29     size_t allocSize = align ? (capacity + align - 1) : capacity;
30     if (this->allocator != nullptr && this->allocator->GetMemoryType() == MemoryType::SHARED_MEMORY) {
31         shareAllocator_ = ReinterpretPointerCast<ShareAllocator>(this->allocator);
32         fd_ = (uintptr_t)shareAllocator_->Alloc(allocSize);
33         sharedMem_ = std::make_shared<Ashmem>(fd_, allocSize);
34         InitShareMemory(shareAllocator_->GetShareMemType());
35     } else {
36         MEDIA_LOG_E("create sharedMem_ failed");
37     }
38 }
39 
~ShareMemory()40 ShareMemory::~ShareMemory()
41 {
42     if (sharedMem_) {
43         sharedMem_->UnmapAshmem();
44         sharedMem_->CloseAshmem();
45         sharedMem_ = nullptr;
46     }
47 }
48 
GetRealAddr() const49 uint8_t* ShareMemory::GetRealAddr() const
50 {
51     auto addr = const_cast<void*>(sharedMem_->ReadFromAshmem(0, 0));
52     return static_cast<uint8_t*>(addr);
53 }
54 
Write(const uint8_t * in,size_t writeSize,size_t position)55 size_t ShareMemory::Write(const uint8_t* in, size_t writeSize, size_t position)
56 {
57     size_t start = 0;
58     if (position == MEM_INVALID_POSITION) {
59         start = size;
60     } else {
61         start = std::min(position, capacity);
62     }
63     size_t length = std::min(writeSize, capacity - start);
64     if (!sharedMem_->WriteToAshmem(in, (int32_t)writeSize, (int32_t)start)) {
65         MEDIA_LOG_E("sharedMem_ WriteToAshmem failed");
66         return 0;
67     }
68     size = start + length;
69     return length;
70 }
71 
Read(uint8_t * out,size_t readSize,size_t position)72 size_t ShareMemory::Read(uint8_t* out, size_t readSize, size_t position)
73 {
74     size_t start = 0;
75     size_t maxLength = size;
76     if (position != MEM_INVALID_POSITION) {
77         start = std::min(position, size);
78         maxLength = size - start;
79     }
80     size_t length = std::min(readSize, maxLength);
81     if (memcpy_s(out, length, sharedMem_->ReadFromAshmem((int32_t)readSize, (int32_t)start), length) != EOK) {
82         return 0;
83     }
84     return length;
85 }
86 
GetShareMemoryFd()87 int ShareMemory::GetShareMemoryFd()
88 {
89     return fd_;
90 }
91 
InitShareMemory(ShareMemType type)92 void ShareMemory::InitShareMemory(ShareMemType type)
93 {
94     switch (type) {
95         case ShareMemType::READ_ONLY_TYPE :
96             if (!sharedMem_->MapReadOnlyAshmem()) {
97                 MEDIA_LOG_E("failed to exec MapReadOnlyAshmem");
98             }
99             break;
100         case ShareMemType::READ_WRITE_TYPE :
101             if (!sharedMem_->MapReadAndWriteAshmem()) {
102                 MEDIA_LOG_E("failed to exec MapReadAndWriteAshmem");
103             }
104             break;
105         default:
106             MEDIA_LOG_E("set share memory type failed, not find this type: " PUBLIC_LOG_D32,
107                 static_cast<int32_t>(type));
108             break;
109     }
110 }
111 } // namespace Plugins
112 } // namespace Media
113 } // namespace OHOS
114 #endif
115 
116