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