1da853ecaSopenharmony_ci/* 2da853ecaSopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd. 3da853ecaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4da853ecaSopenharmony_ci * you may not use this file except in compliance with the License. 5da853ecaSopenharmony_ci * You may obtain a copy of the License at 6da853ecaSopenharmony_ci * 7da853ecaSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8da853ecaSopenharmony_ci * 9da853ecaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10da853ecaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11da853ecaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12da853ecaSopenharmony_ci * See the License for the specific language governing permissions and 13da853ecaSopenharmony_ci * limitations under the License. 14da853ecaSopenharmony_ci */ 15da853ecaSopenharmony_ci 16da853ecaSopenharmony_ci#include <memory> 17da853ecaSopenharmony_ci#include "securec.h" 18da853ecaSopenharmony_ci#include "avcodec_log.h" 19da853ecaSopenharmony_ci#include "avcodec_errors.h" 20da853ecaSopenharmony_ci#include "fsurface_memory.h" 21da853ecaSopenharmony_cinamespace { 22da853ecaSopenharmony_ciconstexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "AvCodec-FSurfaceMemory"}; 23da853ecaSopenharmony_ci} 24da853ecaSopenharmony_cinamespace OHOS { 25da853ecaSopenharmony_cinamespace MediaAVCodec { 26da853ecaSopenharmony_ciFSurfaceMemory::~FSurfaceMemory() 27da853ecaSopenharmony_ci{ 28da853ecaSopenharmony_ci ReleaseSurfaceBuffer(); 29da853ecaSopenharmony_ci} 30da853ecaSopenharmony_ci 31da853ecaSopenharmony_civoid FSurfaceMemory::AllocSurfaceBuffer() 32da853ecaSopenharmony_ci{ 33da853ecaSopenharmony_ci CHECK_AND_RETURN_LOG(sInfo_->surface != nullptr, "surface info is nullptr"); 34da853ecaSopenharmony_ci sptr<SurfaceBuffer> surfaceBuffer = nullptr; 35da853ecaSopenharmony_ci auto ret = sInfo_->surface->RequestBuffer(surfaceBuffer, fence_, sInfo_->requestConfig); 36da853ecaSopenharmony_ci if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK || surfaceBuffer == nullptr) { 37da853ecaSopenharmony_ci if (ret != OHOS::SurfaceError::SURFACE_ERROR_NO_BUFFER) { 38da853ecaSopenharmony_ci AVCODEC_LOGE("surface RequestBuffer fail, ret: %{public}" PRIu64, static_cast<uint64_t>(ret)); 39da853ecaSopenharmony_ci } 40da853ecaSopenharmony_ci return; 41da853ecaSopenharmony_ci } 42da853ecaSopenharmony_ci surfaceBuffer_ = surfaceBuffer; 43da853ecaSopenharmony_ci} 44da853ecaSopenharmony_ci 45da853ecaSopenharmony_civoid FSurfaceMemory::ReleaseSurfaceBuffer() 46da853ecaSopenharmony_ci{ 47da853ecaSopenharmony_ci CHECK_AND_RETURN_LOG(surfaceBuffer_ != nullptr, "surface buffer is nullptr"); 48da853ecaSopenharmony_ci if (!needRender_) { 49da853ecaSopenharmony_ci auto ret = sInfo_->surface->CancelBuffer(surfaceBuffer_); 50da853ecaSopenharmony_ci if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK) { 51da853ecaSopenharmony_ci AVCODEC_LOGE("surface CancelBuffer fail, ret: %{public}" PRIu64, static_cast<uint64_t>(ret)); 52da853ecaSopenharmony_ci } 53da853ecaSopenharmony_ci } 54da853ecaSopenharmony_ci surfaceBuffer_ = nullptr; 55da853ecaSopenharmony_ci} 56da853ecaSopenharmony_ci 57da853ecaSopenharmony_cisptr<SurfaceBuffer> FSurfaceMemory::GetSurfaceBuffer() 58da853ecaSopenharmony_ci{ 59da853ecaSopenharmony_ci if (!surfaceBuffer_) { 60da853ecaSopenharmony_ci AllocSurfaceBuffer(); 61da853ecaSopenharmony_ci } 62da853ecaSopenharmony_ci return surfaceBuffer_; 63da853ecaSopenharmony_ci} 64da853ecaSopenharmony_ci 65da853ecaSopenharmony_ciint32_t FSurfaceMemory::GetSurfaceBufferStride() 66da853ecaSopenharmony_ci{ 67da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(surfaceBuffer_ != nullptr, 0, "surfaceBuffer is nullptr"); 68da853ecaSopenharmony_ci auto bufferHandle = surfaceBuffer_->GetBufferHandle(); 69da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(bufferHandle != nullptr, AVCS_ERR_UNKNOWN, "Fail to get bufferHandle"); 70da853ecaSopenharmony_ci stride_ = bufferHandle->stride; 71da853ecaSopenharmony_ci return stride_; 72da853ecaSopenharmony_ci} 73da853ecaSopenharmony_ci 74da853ecaSopenharmony_cisptr<SyncFence> FSurfaceMemory::GetFence() 75da853ecaSopenharmony_ci{ 76da853ecaSopenharmony_ci return fence_; 77da853ecaSopenharmony_ci} 78da853ecaSopenharmony_ci 79da853ecaSopenharmony_civoid FSurfaceMemory::SetNeedRender(bool needRender) 80da853ecaSopenharmony_ci{ 81da853ecaSopenharmony_ci needRender_ = needRender; 82da853ecaSopenharmony_ci} 83da853ecaSopenharmony_ci 84da853ecaSopenharmony_civoid FSurfaceMemory::UpdateSurfaceBufferScaleMode() 85da853ecaSopenharmony_ci{ 86da853ecaSopenharmony_ci CHECK_AND_RETURN_LOG(surfaceBuffer_ != nullptr, "surface buffer is nullptr"); 87da853ecaSopenharmony_ci auto ret = sInfo_->surface->SetScalingMode(surfaceBuffer_->GetSeqNum(), sInfo_->scalingMode); 88da853ecaSopenharmony_ci if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK) { 89da853ecaSopenharmony_ci AVCODEC_LOGE("update surface buffer scaling mode fail, ret: %{public}" PRIu64, static_cast<uint64_t>(ret)); 90da853ecaSopenharmony_ci } 91da853ecaSopenharmony_ci} 92da853ecaSopenharmony_ci 93da853ecaSopenharmony_ciuint8_t *FSurfaceMemory::GetBase() const 94da853ecaSopenharmony_ci{ 95da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(surfaceBuffer_ != nullptr, nullptr, "surfaceBuffer is nullptr"); 96da853ecaSopenharmony_ci return static_cast<uint8_t *>(surfaceBuffer_->GetVirAddr()); 97da853ecaSopenharmony_ci} 98da853ecaSopenharmony_ci 99da853ecaSopenharmony_ciint32_t FSurfaceMemory::GetSize() const 100da853ecaSopenharmony_ci{ 101da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(surfaceBuffer_ != nullptr, -1, "surfaceBuffer is nullptr"); 102da853ecaSopenharmony_ci uint32_t size = surfaceBuffer_->GetSize(); 103da853ecaSopenharmony_ci return static_cast<int32_t>(size); 104da853ecaSopenharmony_ci} 105da853ecaSopenharmony_ci} // namespace MediaAVCodec 106da853ecaSopenharmony_ci} // namespace OHOS 107