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 "foundation/log.h"
20
21 namespace OHOS {
22 namespace Media {
23 namespace Plugin {
ShareMemory(size_t capacity,std::shared_ptr<Allocator> allocator,size_t align)24 ShareMemory::ShareMemory(size_t capacity, std::shared_ptr<Allocator> allocator, size_t align)
25 : Memory(capacity, std::move(allocator), align, MemoryType::SHARE_MEMORY, false)
26 {
27 size_t allocSize = align ? (capacity + align - 1) : capacity;
28 if (this->allocator != nullptr && this->allocator->GetMemoryType() == MemoryType::SHARE_MEMORY) {
29 shareAllocator_ = ReinterpretPointerCast<ShareAllocator>(this->allocator);
30 fd_ = (uintptr_t)shareAllocator_->Alloc(allocSize);
31 sharedMem_ = std::make_shared<Ashmem>(fd_, allocSize);
32 InitShareMemory(shareAllocator_->GetShareMemType());
33 } else {
34 MEDIA_LOG_E("create sharedMem_ failed");
35 }
36 }
37
~ShareMemory()38 ShareMemory::~ShareMemory()
39 {
40 if (sharedMem_) {
41 sharedMem_->UnmapAshmem();
42 sharedMem_->CloseAshmem();
43 sharedMem_ = nullptr;
44 }
45 }
46
Write(const uint8_t * in,size_t writeSize,size_t position)47 size_t ShareMemory::Write(const uint8_t* in, size_t writeSize, size_t position)
48 {
49 size_t start = 0;
50 if (position == INVALID_POSITION) {
51 start = size;
52 } else {
53 start = std::min(position, capacity);
54 }
55 size_t length = std::min(writeSize, capacity - start);
56 if (!sharedMem_->WriteToAshmem(in, (int32_t)writeSize, (int32_t)start)) {
57 MEDIA_LOG_E("sharedMem_ WriteToAshmem failed");
58 return 0;
59 }
60 size = start + length;
61 return length;
62 }
63
Read(uint8_t * out,size_t readSize,size_t position)64 size_t ShareMemory::Read(uint8_t* out, size_t readSize, size_t position)
65 {
66 size_t start = 0;
67 size_t maxLength = size;
68 if (position != INVALID_POSITION) {
69 start = std::min(position, size);
70 maxLength = size - start;
71 }
72 size_t length = std::min(readSize, maxLength);
73 if (memcpy_s(out, length, sharedMem_->ReadFromAshmem((int32_t)readSize, (int32_t)start), length) != EOK) {
74 return 0;
75 }
76 return length;
77 }
78
GetShareMemoryFd()79 int ShareMemory::GetShareMemoryFd()
80 {
81 return fd_;
82 }
83
InitShareMemory(ShareMemType type)84 void ShareMemory::InitShareMemory(ShareMemType type)
85 {
86 switch (type) {
87 case ShareMemType::READ_ONLY_TYPE :
88 if (!sharedMem_->MapReadOnlyAshmem()) {
89 MEDIA_LOG_E("failed to exec MapReadOnlyAshmem");
90 }
91 break;
92 case ShareMemType::READ_WRITE_TYPE :
93 if (!sharedMem_->MapReadAndWriteAshmem()) {
94 MEDIA_LOG_E("failed to exec MapReadAndWriteAshmem");
95 }
96 break;
97 default:
98 MEDIA_LOG_E("set share memory type failed, not find this type: " PUBLIC_LOG_D32,
99 static_cast<int32_t>(type));
100 break;
101 }
102 }
103 } // namespace Plugin
104 } // namespace Media
105 } // namespace OHOS
106 #endif
107
108