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     return Status::OK;
120 }
121 
InitSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer)122 Status AVSurfaceMemory::InitSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer)
123 {
124     surfaceBuffer_ = surfaceBuffer;
125     capacity_ = static_cast<int32_t>(surfaceBuffer_->GetSize());
126     size_ = capacity_;
127     return Status::OK;
128 }
129 
WriteToMessageParcel(MessageParcel & parcel)130 bool AVSurfaceMemory::WriteToMessageParcel(MessageParcel &parcel)
131 {
132 #ifdef MEDIA_OHOS
133     MessageParcel bufferParcel;
134     GSError gsRet = surfaceBuffer_->WriteToMessageParcel(bufferParcel);
135     FALSE_RETURN_V_MSG_E(gsRet == GSERROR_OK, false, "Write message parcel failed!, %{public}s",
136                          GSErrorStr(gsRet).c_str());
137     size_t size = bufferParcel.GetDataSize();
138     return parcel.WriteUint64(static_cast<uint64_t>(size)) && parcel.Append(bufferParcel);
139 #else
140     return true;
141 #endif
142 }
143 
ReadFromMessageParcel(MessageParcel & parcel)144 bool AVSurfaceMemory::ReadFromMessageParcel(MessageParcel &parcel)
145 {
146 #ifdef MEDIA_OHOS
147     uint64_t size = 0;
148     bool ret = parcel.ReadUint64(size);
149     parcel.SkipBytes(static_cast<size_t>(size));
150     return ret;
151 #else
152     return true;
153 #endif
154 }
155 
GetAddr()156 uint8_t *AVSurfaceMemory::GetAddr()
157 {
158     if (isFirstFlag_) {
159         base_ = reinterpret_cast<uint8_t *>(surfaceBuffer_->GetVirAddr());
160         isFirstFlag_ = false;
161     }
162     return base_;
163 }
164 
GetMemoryType()165 MemoryType AVSurfaceMemory::GetMemoryType()
166 {
167     return MemoryType::SURFACE_MEMORY;
168 }
169 
GetFileDescriptor()170 int32_t AVSurfaceMemory::GetFileDescriptor()
171 {
172     return surfaceBuffer_->GetFileDescriptor();
173 }
174 
GetSurfaceBuffer()175 sptr<SurfaceBuffer> AVSurfaceMemory::GetSurfaceBuffer()
176 {
177     return surfaceBuffer_;
178 }
179 } // namespace Media
180 } // namespace OHOS
181