1 /*
2 * Copyright (C) 2025 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 "surface_tools.h"
17 #include "avcodec_log.h"
18
19 namespace {
20 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "SurfaceTools"};
21 }
22
23 namespace OHOS {
24 namespace MediaAVCodec {
GetInstance()25 SurfaceTools &SurfaceTools::GetInstance()
26 {
27 static SurfaceTools instance;
28 return instance;
29 }
30
RegisterReleaseListener(int32_t instanceId,sptr<Surface> surface,OnReleaseFunc callback,OHSurfaceSource type)31 bool SurfaceTools::RegisterReleaseListener(int32_t instanceId, sptr<Surface> surface, OnReleaseFunc callback,
32 OHSurfaceSource type)
33 {
34 CHECK_AND_RETURN_RET_LOGW(surface != nullptr, false, "Unexpected param");
35
36 uint64_t id = surface->GetUniqueId();
37 std::lock_guard<std::mutex> lock(mutex_);
38 GSError err = surface->RegisterReleaseListener(callback);
39 if (err != GSERROR_OK) {
40 AVCODEC_LOGE("Register Listener failed, GSError=%{public}d, instanceId=%{public}d, id=%{public}" PRIu64,
41 err, instanceId, id);
42 return false;
43 }
44 AVCODEC_LOGI("instanceId=%{public}d register listener to surface id=%{public}" PRIu64,
45 instanceId, id);
46 surface->SetSurfaceSourceType(type);
47 surfaceProducerMap_[id] = instanceId;
48 return true;
49 }
50
CleanCache(int32_t instanceId,sptr<Surface> surface,bool cleanAll)51 void SurfaceTools::CleanCache(int32_t instanceId, sptr<Surface> surface, bool cleanAll)
52 {
53 if (surface == nullptr) {
54 return;
55 }
56 uint64_t id = surface->GetUniqueId();
57 std::lock_guard<std::mutex> lock(mutex_);
58 auto iter = surfaceProducerMap_.find(id);
59 if (iter != surfaceProducerMap_.end() && iter->second == instanceId) {
60 surface->CleanCache(cleanAll);
61 AVCODEC_LOGI("instanceId=%{public}d CleanCache to surface id=%{public}" PRIu64,
62 instanceId, id);
63 }
64 }
65
ReleaseSurface(int32_t instanceId,sptr<Surface> surface,bool cleanAll,bool abadon)66 void SurfaceTools::ReleaseSurface(int32_t instanceId, sptr<Surface> surface, bool cleanAll, bool abadon)
67 {
68 if (surface == nullptr) {
69 return;
70 }
71 uint64_t id = surface->GetUniqueId();
72 std::lock_guard<std::mutex> lock(mutex_);
73 auto iter = surfaceProducerMap_.find(id);
74 if (iter != surfaceProducerMap_.end() && iter->second == instanceId) {
75 surface->CleanCache(cleanAll);
76 AVCODEC_LOGI("instanceId=%{public}d CleanCache to surface id=%{public}" PRIu64,
77 instanceId, id);
78 surface->UnRegisterReleaseListener();
79 AVCODEC_LOGI("instanceId=%{public}d UnRegisterReleaseListener to surface id=%{public}" PRIu64,
80 instanceId, id);
81 surface->SetSurfaceSourceType(OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT);
82 if (abadon) {
83 surfaceProducerMap_.erase(iter);
84 }
85 }
86 }
87 } // namespace MediaAVCodec
88 } // namespace OHOS