• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 int32_t FSurfaceMemory::AllocSurfaceBuffer()
32 {
33     CHECK_AND_RETURN_RET_LOG(sInfo_->surface != nullptr, AVCS_ERR_UNKNOWN, "Surface is nullptr!");
34     CHECK_AND_RETURN_RET_LOG(!isAttached, AVCS_ERR_UNKNOWN, "Only support when not attach!");
35     CHECK_AND_RETURN_RET_LOG(surfaceBuffer_ == nullptr, AVCS_ERR_UNKNOWN, "Surface buffer is not nullptr!");
36     sptr<SurfaceBuffer> surfaceBuffer = SurfaceBuffer::Create();
37     CHECK_AND_RETURN_RET_LOG(surfaceBuffer, AVCS_ERR_UNKNOWN, "Create surface buffer failed!");
38     GSError err = surfaceBuffer->Alloc(sInfo_->requestConfig);
39     CHECK_AND_RETURN_RET_LOG(err == GSERROR_OK, err, "Alloc surface buffer failed, GSERROR=%{public}d", err);
40     SetSurfaceBuffer(surfaceBuffer, Owner::OWNED_BY_CODEC);
41     isAttached = false;
42     AVCODEC_LOGI("Alloc surface buffer success seq=%{public}u", surfaceBuffer_->GetSeqNum());
43     return AVCS_ERR_OK;
44 }
45 
RequestSurfaceBuffer()46 int32_t FSurfaceMemory::RequestSurfaceBuffer()
47 {
48     CHECK_AND_RETURN_RET_LOG(sInfo_->surface != nullptr, AVCS_ERR_UNKNOWN, "Surface is nullptr");
49     CHECK_AND_RETURN_RET_LOG(owner == Owner::OWNED_BY_SURFACE, AVCS_ERR_UNKNOWN, "Only support when owned by surface!");
50     sptr<SurfaceBuffer> surfaceBuffer = nullptr;
51     auto ret = sInfo_->surface->RequestBuffer(surfaceBuffer, fence_, sInfo_->requestConfig);
52     if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK || surfaceBuffer == nullptr) {
53         if (ret != OHOS::SurfaceError::SURFACE_ERROR_NO_BUFFER) {
54             AVCODEC_LOGE("Request surface buffer fail, ret=%{public}" PRIu64, static_cast<uint64_t>(ret));
55         }
56         return ret;
57     }
58     SetSurfaceBuffer(surfaceBuffer, Owner::OWNED_BY_CODEC);
59     return AVCS_ERR_OK;
60 }
61 
ReleaseSurfaceBuffer()62 void FSurfaceMemory::ReleaseSurfaceBuffer()
63 {
64     CHECK_AND_RETURN_LOG(surfaceBuffer_ != nullptr, "Surface buffer is nullptr!");
65     surfaceBuffer_ = nullptr;
66 }
67 
GetSurfaceBuffer()68 sptr<SurfaceBuffer> FSurfaceMemory::GetSurfaceBuffer()
69 {
70     if (isAttached && owner == Owner::OWNED_BY_SURFACE) {
71         CHECK_AND_RETURN_RET_LOG(RequestSurfaceBuffer() == AVCS_ERR_OK, nullptr, "Get surface buffer failed!");
72     }
73     return surfaceBuffer_;
74 }
75 
SetSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer,Owner toChangeOwner)76 void FSurfaceMemory::SetSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer, Owner toChangeOwner)
77 {
78     surfaceBuffer_ = surfaceBuffer;
79     owner = toChangeOwner;
80 }
81 
GetSurfaceBufferStride()82 int32_t FSurfaceMemory::GetSurfaceBufferStride()
83 {
84     CHECK_AND_RETURN_RET_LOG(surfaceBuffer_ != nullptr, 0, "Surface buffer is nullptr!");
85     auto bufferHandle = surfaceBuffer_->GetBufferHandle();
86     CHECK_AND_RETURN_RET_LOG(bufferHandle != nullptr, AVCS_ERR_UNKNOWN, "Failed to get bufferHandle!");
87     stride_ = bufferHandle->stride;
88     return stride_;
89 }
90 
GetFence()91 sptr<SyncFence> FSurfaceMemory::GetFence()
92 {
93     return fence_;
94 }
95 
UpdateSurfaceBufferScaleMode()96 void FSurfaceMemory::UpdateSurfaceBufferScaleMode()
97 {
98     CHECK_AND_RETURN_LOG(surfaceBuffer_ != nullptr, "Surface buffer is nullptr!");
99     auto ret = sInfo_->surface->SetScalingMode(surfaceBuffer_->GetSeqNum(), sInfo_->scalingMode);
100     if (ret != OHOS::SurfaceError::SURFACE_ERROR_OK) {
101         AVCODEC_LOGE("Update surface buffer scaling mode failed, ret=%{public}" PRIu64, static_cast<uint64_t>(ret));
102     }
103 }
104 
GetBase() const105 uint8_t *FSurfaceMemory::GetBase() const
106 {
107     CHECK_AND_RETURN_RET_LOG(surfaceBuffer_ != nullptr, nullptr, "Surface buffer is nullptr!");
108     return static_cast<uint8_t *>(surfaceBuffer_->GetVirAddr());
109 }
110 
GetSize() const111 int32_t FSurfaceMemory::GetSize() const
112 {
113     CHECK_AND_RETURN_RET_LOG(surfaceBuffer_ != nullptr, -1, "Surface buffer is nullptr!");
114     uint32_t size = surfaceBuffer_->GetSize();
115     return static_cast<int32_t>(size);
116 }
117 } // namespace MediaAVCodec
118 } // namespace OHOS
119