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 #ifndef OHOS_HDI_DISPLAY_V1_3_DISPLAY_BUFFER_HDI_IMPL_H 17 #define OHOS_HDI_DISPLAY_V1_3_DISPLAY_BUFFER_HDI_IMPL_H 18 19 #include <iproxy_broker.h> 20 #include <unistd.h> 21 #include "hdf_log.h" 22 #include "hilog/log.h" 23 #include "v1_0/imapper.h" 24 #include "v1_2/imapper.h" 25 #include "v1_3/imapper.h" 26 #include "v1_1/include/idisplay_buffer.h" 27 #include "v1_2/include/idisplay_buffer.h" 28 #include "v1_3/include/idisplay_buffer.h" 29 #include "v1_0/hdi_impl/display_buffer_hdi_impl.h" 30 #include "v1_1/hdi_impl/display_buffer_hdi_impl.h" 31 #include "v1_2/hdi_impl/display_buffer_hdi_impl.h" 32 33 #undef LOG_TAG 34 #define LOG_TAG "DISP_HDI_BUFF" 35 #undef LOG_DOMAIN 36 #define LOG_DOMAIN 0xD002515 37 38 namespace OHOS { 39 namespace HDI { 40 namespace Display { 41 namespace Buffer { 42 namespace V1_3 { 43 template<typename Interface> 44 class DisplayBufferHdiImpl : public V1_2::DisplayBufferHdiImpl<Interface> { 45 public: BaseType3_0(isAllocLocal)46 explicit DisplayBufferHdiImpl(bool isAllocLocal = false) : BaseType3_0(isAllocLocal), mapper_v1_3_(nullptr) 47 { 48 while ((mapper_v1_3_ = IMapper::Get(true)) == nullptr) { 49 // Waiting for metadata service ready 50 usleep(WAIT_TIME_INTERVAL); 51 } 52 } 53 ~DisplayBufferHdiImpl()54 virtual ~DisplayBufferHdiImpl() {}; 55 AllocMemPassThrough(const AllocInfo & info,BufferHandle * & handle)56 int32_t AllocMemPassThrough(const AllocInfo& info, BufferHandle*& handle) const 57 { 58 DISPLAY_TRACE; 59 CHECK_NULLPOINTER_RETURN_VALUE(mapper_v1_3_, HDF_FAILURE); 60 sptr<NativeBuffer> hdiBuffer; 61 int32_t ret = mapper_v1_3_->AllocMem(info, hdiBuffer); 62 if ((ret == HDF_SUCCESS) && (hdiBuffer != nullptr)) { 63 handle = hdiBuffer->Move(); 64 } else { 65 handle = nullptr; 66 if (ret == HDF_SUCCESS) { 67 ret = HDF_FAILURE; 68 } 69 HDF_LOGE("%{public}s: AllocMem error", __func__); 70 } 71 return ret; 72 } 73 AllocMemIpc(const AllocInfo & info,BufferHandle * & handle)74 int32_t AllocMemIpc(const AllocInfo& info, BufferHandle*& handle) const 75 { 76 DISPLAY_TRACE; 77 CHECK_NULLPOINTER_RETURN_VALUE(allocator_, HDF_FAILURE); 78 sptr<NativeBuffer> hdiBuffer; 79 int32_t ret = allocator_->AllocMem(info, hdiBuffer); 80 if ((ret == HDF_SUCCESS) && (hdiBuffer != nullptr)) { 81 handle = hdiBuffer->Move(); 82 } else { 83 handle = nullptr; 84 if (ret == HDF_SUCCESS) { 85 ret = HDF_FAILURE; 86 } 87 HDF_LOGE("%{public}s: AllocMem error", __func__); 88 } 89 return ret; 90 } 91 AllocMem(const AllocInfo & info,BufferHandle * & handle)92 int32_t AllocMem(const AllocInfo& info, BufferHandle*& handle) const override 93 { 94 DISPLAY_TRACE; 95 CHECK_NULLPOINTER_RETURN_VALUE(mapper_v1_3_, HDF_FAILURE); 96 97 //Step1. check is support alloc passthrough 98 if (mapper_v1_3_->IsSupportAllocPassthrough(info) == HDF_SUCCESS) { 99 int32_t ret = AllocMemPassThrough(info, handle); 100 if (ret != HDF_SUCCESS) { 101 HDF_LOGW("%{public}s: AllocMem Passthrough mode failed, use allocator_host", __func__); 102 return AllocMemIpc(info, handle); 103 } 104 return ret; 105 } else { 106 return AllocMemIpc(info, handle); 107 } 108 } 109 ReAllocMem(const V1_0::AllocInfo & info,const BufferHandle & inHandle,BufferHandle * & outHandle)110 virtual int32_t ReAllocMem(const V1_0::AllocInfo& info, const BufferHandle& inHandle, 111 BufferHandle*& outHandle)const override 112 { 113 DISPLAY_TRACE; 114 CHECK_NULLPOINTER_RETURN_VALUE(mapper_v1_3_, HDF_FAILURE); 115 116 sptr<NativeBuffer> hdiInBuffer = new NativeBuffer(); 117 CHECK_NULLPOINTER_RETURN_VALUE(hdiInBuffer, HDF_FAILURE); 118 sptr<NativeBuffer> hdiOutBuffer; 119 120 hdiInBuffer->SetBufferHandle(const_cast<BufferHandle*>(&inHandle)); 121 int32_t ret = mapper_v1_3_->ReAllocMem(info, hdiInBuffer, hdiOutBuffer); 122 if ((ret == HDF_SUCCESS) && (hdiOutBuffer != nullptr)) { 123 outHandle = hdiOutBuffer->Move(); 124 } else { 125 return AllocMemIpc(info, outHandle); 126 } 127 return ret; 128 } 129 130 private: 131 using BaseType3_0 = V1_2::DisplayBufferHdiImpl<Interface>; 132 protected: 133 using BaseType3_0::WAIT_TIME_INTERVAL; 134 using BaseType3_0::allocator_; 135 sptr<IMapper> mapper_v1_3_; 136 }; 137 using HdiDisplayBufferImpl = DisplayBufferHdiImpl<V1_3::IDisplayBuffer>; 138 } // namespace V1_3 139 } // namespace Buffer 140 } // namespace Display 141 } // namespace HDI 142 } // namespace OHOS 143 144 #endif // OHOS_HDI_DISPLAY_V1_3_DISPLAY_BUFFER_HDI_IMPL_H 145