• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef OHOS_HDI_DISPLAY_V1_0_BUFFER_CACHE_UTILS_H
17 #define OHOS_HDI_DISPLAY_V1_0_BUFFER_CACHE_UTILS_H
18 
19 #include <functional>
20 #include <memory>
21 #include <unordered_map>
22 #include "base/native_buffer.h"
23 #include "cache_manager.h"
24 #include "common/include/display_interface_utils.h"
25 #include "hdf_log.h"
26 
27 namespace OHOS {
28 namespace HDI {
29 namespace Display {
30 namespace Composer {
31 namespace V1_0 {
32 
33 #define DEBUG_BUFFER_CACHE_UTILS
34 class BufferCacheUtils {
35 public:
NativeBufferCache(const std::shared_ptr<CacheManager<uint32_t,NativeBuffer>> & cacheMgr,BufferHandle * buffer,uint32_t seqNo,uint32_t callerId)36     static BufferHandle* NativeBufferCache(const std::shared_ptr<CacheManager<uint32_t, NativeBuffer>>& cacheMgr,
37         BufferHandle* buffer, uint32_t seqNo, uint32_t callerId)
38     {
39         BufferHandle* handle = nullptr;
40         if (buffer == nullptr && seqNo != UINT32_MAX) {
41             // Fetch buffer from caches indexed by seqNo
42             NativeBuffer* nativeBuffer = cacheMgr->SearchCache(seqNo);
43             DISPLAY_CHK_RETURN(nativeBuffer == nullptr, handle,
44                 HDF_LOGE("%{public}s: Get Native buffer cache is not found, callerId=%{public}u, seqNo=%{public}u",
45                     __func__, callerId, seqNo));
46 
47             handle = nativeBuffer->GetBufferHandle();
48             HDF_LOGD("apply %{public}s: callerId=%{public}u, seqNo=%{public}u", __func__, callerId, seqNo);
49             DISPLAY_CHK_RETURN(handle == nullptr, handle,
50                 HDF_LOGE("%{public}s: Get buffer cache is nullptr, callerId=%{public}u, seqNo=%{public}u",
51                     __func__, callerId, seqNo));
52         } else if (buffer != nullptr && seqNo != UINT32_MAX) {
53             // Insert buffer to caches
54             NativeBuffer* nativeBuffer = new NativeBuffer();
55             DISPLAY_CHK_RETURN(nativeBuffer == nullptr, nullptr,
56                 HDF_LOGE("%{public}s: new nativeBuffer fail", __func__));
57             nativeBuffer->SetBufferHandle(buffer, true, nullptr);
58 
59             auto retBool = cacheMgr->InsertCache(seqNo, nativeBuffer);
60             if (retBool == false) {
61                 // if InsertCache failed, remove BufferHandle ownership
62                 handle = nativeBuffer->Move();
63                 delete nativeBuffer;
64                 HDF_LOGE("%{public}s: Set buffer cache fail, callerId=%{public}u, seqNo=%{public}u",
65                     __func__, callerId, seqNo);
66             } else {
67                 handle = buffer;
68             }
69             HDF_LOGD("insert %{public}s: callerId=%{public}u, seqNo=%{public}u", __func__, callerId, seqNo);
70         } else if (buffer != nullptr && seqNo == UINT32_MAX) {
71             // Caches not used
72             HDF_LOGI("%{public}s: buffer cache passthrough", __func__);
73             handle = buffer;
74         } else {
75             // Input arguments error
76             DISPLAY_CHK_RETURN(((buffer == nullptr)), nullptr,
77                 HDF_LOGE("%{public}s: Inputs args check error", __func__));
78         }
79 
80         return handle;
81     }
82 };
83 } // namespace V1_0
84 } // namespace Composer
85 } // namespace Display
86 } // namespace HDI
87 } // namespace OHOS
88 #endif // OHOS_HDI_DISPLAY_V1_0_BUFFER_CACHE_UTILS_H
89