• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #include "av_surface_memory.h"
17 #include <unistd.h>
18 #include "av_surface_allocator.h"
19 #include "common/log.h"
20 #include "common/status.h"
21 #include "message_parcel.h"
22 #include "scope_guard.h"
23 #include "surface_buffer.h"
24 #include "surface_type.h"
25 
26 namespace OHOS {
27 namespace Media {
CreateSurfaceAllocator(const BufferRequestConfig & config)28 std::shared_ptr<AVAllocator> AVAllocatorFactory::CreateSurfaceAllocator(const BufferRequestConfig &config)
29 {
30     auto allocator = std::shared_ptr<AVSurfaceAllocator>(new AVSurfaceAllocator());
31     FALSE_RETURN_V_MSG_E(allocator != nullptr, nullptr, "Create AVSurfaceAllocator failed, no memory");
32     allocator->config_ = config;
33     return allocator;
34 }
35 
AVSurfaceAllocator()36 AVSurfaceAllocator::AVSurfaceAllocator() {}
37 
Alloc(int32_t capacity)38 void *AVSurfaceAllocator::Alloc(int32_t capacity)
39 {
40     (void)capacity;
41     sptr<SurfaceBuffer> surfaceBuffer = SurfaceBuffer::Create();
42     FALSE_RETURN_V_MSG_E(surfaceBuffer != nullptr, nullptr, "No memory for new SurfaceBuffer!");
43     GSError ret = surfaceBuffer->Alloc(config_);
44     FALSE_RETURN_V_MSG_E(ret == GSERROR_OK, nullptr, "Surface Buffer Alloc failed, %{public}s",
45                          GSErrorStr(ret).c_str());
46     surfaceBuffer->IncStrongRef(surfaceBuffer.GetRefPtr());
47     return static_cast<void *>(surfaceBuffer.GetRefPtr());
48 }
49 
Free(void * ptr)50 bool AVSurfaceAllocator::Free(void *ptr)
51 {
52     FALSE_RETURN_V_MSG_E(ptr != nullptr, false, "ptr is nullptr");
53 
54     sptr<SurfaceBuffer> surfaceBuffer = sptr<SurfaceBuffer>(static_cast<SurfaceBuffer *>(ptr));
55     surfaceBuffer->DecStrongRef(surfaceBuffer.GetRefPtr());
56 
57     MEDIA_LOG_DD("GetSptrRefCount:%{public}d", surfaceBuffer->GetSptrRefCount());
58     surfaceBuffer = nullptr;
59     return true;
60 }
61 
GetMemoryType()62 MemoryType AVSurfaceAllocator::GetMemoryType()
63 {
64     return MemoryType::SURFACE_MEMORY;
65 }
66 
AVSurfaceMemory()67 AVSurfaceMemory::AVSurfaceMemory() : isFirstFlag_(true) {}
68 
~AVSurfaceMemory()69 AVSurfaceMemory::~AVSurfaceMemory()
70 {
71     if (base_ != nullptr) {
72         surfaceBuffer_->Unmap();
73         base_ = nullptr;
74     }
75     if (allocator_ != nullptr) {
76         bool ret = allocator_->Free(surfaceBuffer_.GetRefPtr());
77         FALSE_RETURN_MSG(ret, "Free memory failed, instance: 0x%{public}06" PRIXPTR, FAKE_POINTER(this));
78     }
79     surfaceBuffer_ = nullptr;
80 }
81 
Init()82 Status AVSurfaceMemory::Init()
83 {
84     surfaceBuffer_ = sptr<SurfaceBuffer>(static_cast<SurfaceBuffer *>(allocator_->Alloc(0)));
85     FALSE_RETURN_V_MSG_E(surfaceBuffer_ != nullptr, Status::ERROR_NO_MEMORY, "surfaceBuffer_ alloc failed");
86     capacity_ = surfaceBuffer_->GetSize();
87 
88     return Status::OK;
89 }
90 
Init(MessageParcel & parcel)91 Status AVSurfaceMemory::Init(MessageParcel &parcel)
92 {
93     (void)parcel.ReadUint64();
94     return InitSurfaceBuffer(parcel);
95 }
96 
InitSurfaceBuffer(MessageParcel & parcel)97 Status AVSurfaceMemory::InitSurfaceBuffer(MessageParcel &parcel)
98 {
99     surfaceBuffer_ = SurfaceBuffer::Create();
100     FALSE_RETURN_V_MSG_E(surfaceBuffer_ != nullptr, Status::ERROR_NO_MEMORY, "No memory for new SurfaceBuffer!");
101 
102     GSError ret = surfaceBuffer_->ReadFromMessageParcel(parcel);
103     FALSE_RETURN_V_MSG_E(ret == GSERROR_OK, Status::ERROR_INVALID_OPERATION, "Read message parcel failed! %{public}s",
104                          GSErrorStr(ret).c_str());
105     capacity_ = surfaceBuffer_->GetSize();
106 
107     return Status::OK;
108 }
109 
InitSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer)110 Status AVSurfaceMemory::InitSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer)
111 {
112     surfaceBuffer_ = surfaceBuffer;
113     capacity_ = surfaceBuffer_->GetSize();
114 
115     return Status::OK;
116 }
117 
WriteToMessageParcel(MessageParcel & parcel)118 bool AVSurfaceMemory::WriteToMessageParcel(MessageParcel &parcel)
119 {
120 #ifdef MEDIA_OHOS
121     MessageParcel bufferParcel;
122     GSError gsRet = surfaceBuffer_->WriteToMessageParcel(bufferParcel);
123     FALSE_RETURN_V_MSG_E(gsRet == GSERROR_OK, false, "Write message parcel failed!, %{public}s",
124                          GSErrorStr(gsRet).c_str());
125     size_t size = bufferParcel.GetDataSize();
126     return parcel.WriteUint64(static_cast<uint64_t>(size)) && parcel.Append(bufferParcel);
127 #else
128     return true;
129 #endif
130 }
131 
ReadFromMessageParcel(MessageParcel & parcel)132 bool AVSurfaceMemory::ReadFromMessageParcel(MessageParcel &parcel)
133 {
134 #ifdef MEDIA_OHOS
135     uint64_t size = 0;
136     bool ret = parcel.ReadUint64(size);
137     parcel.SkipBytes(static_cast<size_t>(size));
138     return ret;
139 #else
140     return true;
141 #endif
142 }
143 
GetAddr()144 uint8_t *AVSurfaceMemory::GetAddr()
145 {
146     if (isFirstFlag_) {
147         Status ret = MapMemoryAddr();
148         FALSE_RETURN_V_MSG_E(ret == Status::OK, nullptr, "MapMemory failed");
149         isFirstFlag_ = false;
150     }
151     return base_;
152 }
153 
GetMemoryType()154 MemoryType AVSurfaceMemory::GetMemoryType()
155 {
156     return MemoryType::SURFACE_MEMORY;
157 }
158 
GetFileDescriptor()159 int32_t AVSurfaceMemory::GetFileDescriptor()
160 {
161     return surfaceBuffer_->GetFileDescriptor();
162 }
163 
GetSurfaceBuffer()164 sptr<SurfaceBuffer> AVSurfaceMemory::GetSurfaceBuffer()
165 {
166     return surfaceBuffer_;
167 }
168 
UnMapMemoryAddr()169 void AVSurfaceMemory::UnMapMemoryAddr()
170 {
171     if (base_ != nullptr) {
172         surfaceBuffer_->Unmap();
173         base_ = nullptr;
174     }
175 }
176 
MapMemoryAddr()177 Status AVSurfaceMemory::MapMemoryAddr()
178 {
179     ON_SCOPE_EXIT(0)
180     {
181         MEDIA_LOG_E("create avsurfacememory failed, uid:" PUBLIC_LOG_U64 ", capacity:%{public}d", uid_, capacity_);
182         UnMapMemoryAddr();
183         return Status::ERROR_NO_MEMORY;
184     };
185     GSError ret = surfaceBuffer_->Map();
186     FALSE_RETURN_V_MSG_E(ret == GSERROR_OK, Status::ERROR_INVALID_OPERATION,
187                          "mmap failed, please check params, %{public}s", GSErrorStr(ret).c_str());
188     base_ = reinterpret_cast<uint8_t *>(surfaceBuffer_->GetVirAddr());
189     CANCEL_SCOPE_EXIT_GUARD(0);
190     return Status::OK;
191 }
192 } // namespace Media
193 } // namespace OHOS
194