1 /*
2  * Copyright (c) 2023-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 <memory>
17 #include "securec.h"
18 #include "avcodec_log.h"
19 #include "avcodec_errors.h"
20 #include "fsurface_memory.h"
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "AvCodec-FSurfaceMemory"};
23 }
24 namespace OHOS {
25 namespace MediaAVCodec {
~FSurfaceMemory()26 FSurfaceMemory::~FSurfaceMemory()
27 {
28     ReleaseSurfaceBuffer();
29 }
30 
AllocSurfaceBuffer()31 void FSurfaceMemory::AllocSurfaceBuffer()
32 {
33     CHECK_AND_RETURN_LOG(sInfo_->surface != nullptr, "surface info is nullptr");
34     sptr<SurfaceBuffer> surfaceBuffer = nullptr;
35     auto ret = sInfo_->surface->RequestBuffer(surfaceBuffer, fence_, sInfo_->requestConfig);
36     if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK || surfaceBuffer == nullptr) {
37         if (ret != OHOS::SurfaceError::SURFACE_ERROR_NO_BUFFER) {
38             AVCODEC_LOGE("surface RequestBuffer fail, ret: %{public}" PRIu64, static_cast<uint64_t>(ret));
39         }
40         return;
41     }
42     surfaceBuffer_ = surfaceBuffer;
43 }
44 
ReleaseSurfaceBuffer()45 void FSurfaceMemory::ReleaseSurfaceBuffer()
46 {
47     CHECK_AND_RETURN_LOG(surfaceBuffer_ != nullptr, "surface buffer is nullptr");
48     if (!needRender_) {
49         auto ret = sInfo_->surface->CancelBuffer(surfaceBuffer_);
50         if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK) {
51             AVCODEC_LOGE("surface CancelBuffer fail, ret:  %{public}" PRIu64, static_cast<uint64_t>(ret));
52         }
53     }
54     surfaceBuffer_ = nullptr;
55 }
56 
GetSurfaceBuffer()57 sptr<SurfaceBuffer> FSurfaceMemory::GetSurfaceBuffer()
58 {
59     if (!surfaceBuffer_) {
60         AllocSurfaceBuffer();
61     }
62     return surfaceBuffer_;
63 }
64 
GetSurfaceBufferStride()65 int32_t FSurfaceMemory::GetSurfaceBufferStride()
66 {
67     CHECK_AND_RETURN_RET_LOG(surfaceBuffer_ != nullptr, 0, "surfaceBuffer is nullptr");
68     auto bufferHandle = surfaceBuffer_->GetBufferHandle();
69     CHECK_AND_RETURN_RET_LOG(bufferHandle != nullptr, AVCS_ERR_UNKNOWN, "Fail to get bufferHandle");
70     stride_ = bufferHandle->stride;
71     return stride_;
72 }
73 
GetFence()74 sptr<SyncFence> FSurfaceMemory::GetFence()
75 {
76     return fence_;
77 }
78 
SetNeedRender(bool needRender)79 void FSurfaceMemory::SetNeedRender(bool needRender)
80 {
81     needRender_ = needRender;
82 }
83 
UpdateSurfaceBufferScaleMode()84 void FSurfaceMemory::UpdateSurfaceBufferScaleMode()
85 {
86     CHECK_AND_RETURN_LOG(surfaceBuffer_ != nullptr, "surface buffer is nullptr");
87     auto ret = sInfo_->surface->SetScalingMode(surfaceBuffer_->GetSeqNum(), sInfo_->scalingMode);
88     if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK) {
89         AVCODEC_LOGE("update surface buffer scaling mode fail, ret: %{public}" PRIu64, static_cast<uint64_t>(ret));
90     }
91 }
92 
GetBase() const93 uint8_t *FSurfaceMemory::GetBase() const
94 {
95     CHECK_AND_RETURN_RET_LOG(surfaceBuffer_ != nullptr, nullptr, "surfaceBuffer is nullptr");
96     return static_cast<uint8_t *>(surfaceBuffer_->GetVirAddr());
97 }
98 
GetSize() const99 int32_t FSurfaceMemory::GetSize() const
100 {
101     CHECK_AND_RETURN_RET_LOG(surfaceBuffer_ != nullptr, -1, "surfaceBuffer is nullptr");
102     uint32_t size = surfaceBuffer_->GetSize();
103     return static_cast<int32_t>(size);
104 }
105 } // namespace MediaAVCodec
106 } // namespace OHOS
107