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 {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "AVSurfaceMemory" };
28 }
29 
30 namespace OHOS {
31 namespace Media {
CreateSurfaceAllocator(const BufferRequestConfig &config)32 std::shared_ptr<AVAllocator> AVAllocatorFactory::CreateSurfaceAllocator(const BufferRequestConfig &config)
33 {
34     auto allocator = std::shared_ptr<AVSurfaceAllocator>(new AVSurfaceAllocator());
35     FALSE_RETURN_V_MSG_E(allocator != nullptr, nullptr, "Create AVSurfaceAllocator failed, no memory");
36     allocator->config_ = config;
37     return allocator;
38 }
39 
AVSurfaceAllocator()40 AVSurfaceAllocator::AVSurfaceAllocator()
41 {
42     config_ = {
43         .width = 0,
44         .height = 0,
45         .strideAlignment = 0x0,
46         .format = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_RGBA_8888,
47         .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
48         .timeout = 0,
49     };
50 }
51 
Alloc(int32_t capacity)52 void *AVSurfaceAllocator::Alloc(int32_t capacity)
53 {
54     (void)capacity;
55     sptr<SurfaceBuffer> surfaceBuffer = SurfaceBuffer::Create();
56     FALSE_RETURN_V_MSG_E(surfaceBuffer != nullptr, nullptr, "No memory for new SurfaceBuffer!");
57     GSError ret = surfaceBuffer->Alloc(config_);
58     FALSE_RETURN_V_MSG_E(ret == GSERROR_OK, nullptr, "Surface Buffer Alloc failed, %{public}s",
59                          GSErrorStr(ret).c_str());
60     surfaceBuffer->IncStrongRef(surfaceBuffer.GetRefPtr());
61     return static_cast<void *>(surfaceBuffer.GetRefPtr());
62 }
63 
Free(void *ptr)64 bool AVSurfaceAllocator::Free(void *ptr)
65 {
66     FALSE_RETURN_V_MSG_E(ptr != nullptr, false, "ptr is nullptr");
67 
68     sptr<SurfaceBuffer> surfaceBuffer = sptr<SurfaceBuffer>(static_cast<SurfaceBuffer *>(ptr));
69     surfaceBuffer->DecStrongRef(surfaceBuffer.GetRefPtr());
70 
71     MEDIA_LOG_DD("GetSptrRefCount:%{public}d", surfaceBuffer->GetSptrRefCount());
72     surfaceBuffer = nullptr;
73     return true;
74 }
75 
GetMemoryType()76 MemoryType AVSurfaceAllocator::GetMemoryType()
77 {
78     return MemoryType::SURFACE_MEMORY;
79 }
80 
AVSurfaceMemory()81 AVSurfaceMemory::AVSurfaceMemory() : isFirstFlag_(true) {}
82 
~AVSurfaceMemory()83 AVSurfaceMemory::~AVSurfaceMemory()
84 {
85     if (base_ != nullptr) {
86         base_ = nullptr;
87     }
88     if (allocator_ != nullptr) {
89         bool ret = allocator_->Free(surfaceBuffer_.GetRefPtr());
90         FALSE_RETURN_MSG(ret, "Free memory failed, instance: 0x%{public}06" PRIXPTR, FAKE_POINTER(this));
91     }
92     surfaceBuffer_ = nullptr;
93 }
94 
Init()95 Status AVSurfaceMemory::Init()
96 {
97     surfaceBuffer_ = sptr<SurfaceBuffer>(static_cast<SurfaceBuffer *>(allocator_->Alloc(0)));
98     FALSE_RETURN_V_MSG_E(surfaceBuffer_ != nullptr, Status::ERROR_NO_MEMORY, "surfaceBuffer_ alloc failed");
99     capacity_ = static_cast<int32_t>(surfaceBuffer_->GetSize());
100 
101     return Status::OK;
102 }
103 
Init(MessageParcel &parcel)104 Status AVSurfaceMemory::Init(MessageParcel &parcel)
105 {
106     (void)parcel.ReadUint64();
107     return InitSurfaceBuffer(parcel);
108 }
109 
InitSurfaceBuffer(MessageParcel &parcel)110 Status AVSurfaceMemory::InitSurfaceBuffer(MessageParcel &parcel)
111 {
112     surfaceBuffer_ = SurfaceBuffer::Create();
113     FALSE_RETURN_V_MSG_E(surfaceBuffer_ != nullptr, Status::ERROR_NO_MEMORY, "No memory for new SurfaceBuffer!");
114 
115     GSError ret = surfaceBuffer_->ReadFromMessageParcel(parcel);
116     FALSE_RETURN_V_MSG_E(ret == GSERROR_OK, Status::ERROR_INVALID_OPERATION, "Read message parcel failed! %{public}s",
117                          GSErrorStr(ret).c_str());
118     capacity_ = static_cast<int32_t>(surfaceBuffer_->GetSize());
119     size_ = capacity_;
120     return Status::OK;
121 }
122 
InitSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer)123 Status AVSurfaceMemory::InitSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer)
124 {
125     surfaceBuffer_ = surfaceBuffer;
126     capacity_ = static_cast<int32_t>(surfaceBuffer_->GetSize());
127     size_ = capacity_;
128     return Status::OK;
129 }
130 
WriteToMessageParcel(MessageParcel &parcel)131 bool AVSurfaceMemory::WriteToMessageParcel(MessageParcel &parcel)
132 {
133 #ifdef MEDIA_OHOS
134     MessageParcel bufferParcel;
135     GSError gsRet = surfaceBuffer_->WriteToMessageParcel(bufferParcel);
136     FALSE_RETURN_V_MSG_E(gsRet == GSERROR_OK, false, "Write message parcel failed!, %{public}s",
137                          GSErrorStr(gsRet).c_str());
138     size_t size = bufferParcel.GetDataSize();
139     return parcel.WriteUint64(static_cast<uint64_t>(size)) && parcel.Append(bufferParcel);
140 #else
141     return true;
142 #endif
143 }
144 
ReadFromMessageParcel(MessageParcel &parcel)145 bool AVSurfaceMemory::ReadFromMessageParcel(MessageParcel &parcel)
146 {
147 #ifdef MEDIA_OHOS
148     uint64_t size = 0;
149     bool ret = parcel.ReadUint64(size);
150     parcel.SkipBytes(static_cast<size_t>(size));
151     return ret;
152 #else
153     return true;
154 #endif
155 }
156 
GetAddr()157 uint8_t *AVSurfaceMemory::GetAddr()
158 {
159     if (isFirstFlag_) {
160         base_ = reinterpret_cast<uint8_t *>(surfaceBuffer_->GetVirAddr());
161         isFirstFlag_ = false;
162     }
163     return base_;
164 }
165 
GetMemoryType()166 MemoryType AVSurfaceMemory::GetMemoryType()
167 {
168     return MemoryType::SURFACE_MEMORY;
169 }
170 
GetFileDescriptor()171 int32_t AVSurfaceMemory::GetFileDescriptor()
172 {
173     return surfaceBuffer_->GetFileDescriptor();
174 }
175 
GetSurfaceBuffer()176 sptr<SurfaceBuffer> AVSurfaceMemory::GetSurfaceBuffer()
177 {
178     return surfaceBuffer_;
179 }
180 } // namespace Media
181 } // namespace OHOS
182