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_DISPLAY_BUFFER_HDI_IMPL_H 17 #define OHOS_HDI_DISPLAY_V1_0_DISPLAY_BUFFER_HDI_IMPL_H 18 19 #include <iremote_object.h> 20 #include <iproxy_broker.h> 21 #include <unistd.h> 22 #include "hdf_log.h" 23 #include "hilog/log.h" 24 #include "buffer_handle.h" 25 #include "v1_0/display_buffer_type.h" 26 #include "v1_0/iallocator.h" 27 #include "v1_0/imapper.h" 28 #include "v1_0/include/idisplay_buffer.h" 29 #include "hdf_trace.h" 30 31 #undef LOG_TAG 32 #define LOG_TAG "DISP_HDI_BUFF" 33 #undef LOG_DOMAIN 34 #define LOG_DOMAIN 0xD002515 35 #define DISPLAY_TRACE HdfTrace trace(__func__, "HDI:DISP:IMPL:") 36 37 #ifndef BUFFER_HDI_IMPL_LOGE 38 #define BUFFER_HDI_IMPL_LOGE(format, ...) \ 39 do { \ 40 HDF_LOGE( \ 41 "\033[0;32;31m" \ 42 "[%{public}s:%{public}d] " format "\033[m" \ 43 "\n", \ 44 __FUNCTION__, __LINE__, ##__VA_ARGS__); \ 45 } while (0) 46 #endif 47 48 #ifndef CHECK_NULLPOINTER_RETURN_VALUE 49 #define CHECK_NULLPOINTER_RETURN_VALUE(pointer, ret) \ 50 do { \ 51 if ((pointer) == NULL) { \ 52 BUFFER_HDI_IMPL_LOGE("pointer is null and return ret\n"); \ 53 return (ret); \ 54 } \ 55 } while (0) 56 #endif 57 58 #ifndef CHECK_NULLPOINTER_RETURN 59 #define CHECK_NULLPOINTER_RETURN(pointer) \ 60 do { \ 61 if ((pointer) == NULL) { \ 62 BUFFER_HDI_IMPL_LOGE("pointer is null and return\n"); \ 63 return; \ 64 } \ 65 } while (0) 66 #endif 67 68 namespace OHOS { 69 namespace HDI { 70 namespace Display { 71 namespace Buffer { 72 namespace V1_0 { 73 74 template<typename Interface> 75 class DisplayBufferHdiImpl : public Interface { 76 public: allocator_(nullptr)77 explicit DisplayBufferHdiImpl(bool isAllocLocal = false) : allocator_(nullptr), 78 mapper_(nullptr), recipient_(nullptr) 79 { 80 while ((allocator_ = IAllocator::Get(isAllocLocal)) == nullptr) { 81 // Waiting for allocator service ready 82 usleep(WAIT_TIME_INTERVAL); 83 } 84 while ((mapper_ = IMapper::Get(true)) == nullptr) { 85 // Waiting for mapper IF ready 86 usleep(WAIT_TIME_INTERVAL); 87 } 88 } ~DisplayBufferHdiImpl()89 virtual ~DisplayBufferHdiImpl() 90 { 91 if (recipient_ != nullptr) { 92 sptr<IRemoteObject> remoteObj = OHOS::HDI::hdi_objcast<IAllocator>(allocator_); 93 remoteObj->RemoveDeathRecipient(recipient_); 94 recipient_ = nullptr; 95 } 96 } 97 AddDeathRecipient(const sptr<IRemoteObject::DeathRecipient> & recipient)98 bool AddDeathRecipient(const sptr<IRemoteObject::DeathRecipient>& recipient) override 99 { 100 sptr<IRemoteObject> remoteObj = OHOS::HDI::hdi_objcast<IAllocator>(allocator_); 101 if (recipient_ != nullptr) { 102 HDF_LOGE("%{public}s: the existing recipient is removed, and add the new. %{public}d", 103 __func__, __LINE__); 104 remoteObj->RemoveDeathRecipient(recipient_); 105 } 106 bool ret = remoteObj->AddDeathRecipient(recipient); 107 if (ret) { 108 recipient_ = recipient; 109 } else { 110 recipient_ = nullptr; 111 HDF_LOGE("%{public}s: AddDeathRecipient failed %{public}d", __func__, __LINE__); 112 } 113 return ret; 114 } 115 RemoveDeathRecipient()116 bool RemoveDeathRecipient() override 117 { 118 if (recipient_ != nullptr) { 119 sptr<IRemoteObject> remoteObj = OHOS::HDI::hdi_objcast<IAllocator>(allocator_); 120 remoteObj->RemoveDeathRecipient(recipient_); 121 recipient_ = nullptr; 122 } 123 return true; 124 } 125 AllocMem(const AllocInfo & info,BufferHandle * & handle)126 int32_t AllocMem(const AllocInfo& info, BufferHandle*& handle) const override 127 { 128 DISPLAY_TRACE; 129 CHECK_NULLPOINTER_RETURN_VALUE(allocator_, HDF_FAILURE); 130 sptr<NativeBuffer> hdiBuffer; 131 int32_t ret = allocator_->AllocMem(info, hdiBuffer); 132 if ((ret == HDF_SUCCESS) && (hdiBuffer != nullptr)) { 133 handle = hdiBuffer->Move(); 134 } else { 135 handle = nullptr; 136 if (ret == HDF_SUCCESS) { 137 ret = HDF_FAILURE; 138 } 139 HDF_LOGE("%{public}s: AllocMem error", __func__); 140 } 141 return ret; 142 } 143 FreeMem(const BufferHandle & handle)144 void FreeMem(const BufferHandle& handle) const override 145 { 146 CHECK_NULLPOINTER_RETURN(mapper_); 147 sptr<NativeBuffer> hdiBuffer = new NativeBuffer(); 148 CHECK_NULLPOINTER_RETURN(hdiBuffer); 149 hdiBuffer->SetBufferHandle(const_cast<BufferHandle*>(&handle), true); 150 mapper_->FreeMem(hdiBuffer); 151 } 152 Mmap(const BufferHandle & handle)153 void *Mmap(const BufferHandle& handle) const override 154 { 155 CHECK_NULLPOINTER_RETURN_VALUE(mapper_, nullptr); 156 sptr<NativeBuffer> hdiBuffer = new NativeBuffer(); 157 CHECK_NULLPOINTER_RETURN_VALUE(hdiBuffer, nullptr); 158 hdiBuffer->SetBufferHandle(const_cast<BufferHandle*>(&handle)); 159 int32_t ret = mapper_->Mmap(hdiBuffer); 160 void *virAddr = (ret == HDF_SUCCESS ? handle.virAddr : nullptr); 161 return virAddr; 162 } 163 Unmap(const BufferHandle & handle)164 int32_t Unmap(const BufferHandle& handle) const override 165 { 166 CHECK_NULLPOINTER_RETURN_VALUE(mapper_, HDF_FAILURE); 167 sptr<NativeBuffer> hdiBuffer = new NativeBuffer(); 168 CHECK_NULLPOINTER_RETURN_VALUE(hdiBuffer, HDF_FAILURE); 169 hdiBuffer->SetBufferHandle(const_cast<BufferHandle*>(&handle)); 170 int32_t ret = mapper_->Unmap(hdiBuffer); 171 return ret; 172 } 173 FlushCache(const BufferHandle & handle)174 int32_t FlushCache(const BufferHandle& handle) const override 175 { 176 CHECK_NULLPOINTER_RETURN_VALUE(mapper_, HDF_FAILURE); 177 sptr<NativeBuffer> hdiBuffer = new NativeBuffer(); 178 CHECK_NULLPOINTER_RETURN_VALUE(hdiBuffer, HDF_FAILURE); 179 hdiBuffer->SetBufferHandle(const_cast<BufferHandle*>(&handle)); 180 int32_t ret = mapper_->FlushCache(hdiBuffer); 181 return ret; 182 } 183 InvalidateCache(const BufferHandle & handle)184 int32_t InvalidateCache(const BufferHandle& handle) const override 185 { 186 CHECK_NULLPOINTER_RETURN_VALUE(mapper_, HDF_FAILURE); 187 sptr<NativeBuffer> hdiBuffer = new NativeBuffer(); 188 CHECK_NULLPOINTER_RETURN_VALUE(hdiBuffer, HDF_FAILURE); 189 hdiBuffer->SetBufferHandle(const_cast<BufferHandle*>(&handle)); 190 int32_t ret = mapper_->InvalidateCache(hdiBuffer); 191 return ret; 192 } 193 IsSupportedAlloc(const std::vector<VerifyAllocInfo> & infos,std::vector<bool> & supporteds)194 int32_t IsSupportedAlloc(const std::vector<VerifyAllocInfo>& infos, 195 std::vector<bool>& supporteds) const override 196 { 197 (void)infos; 198 (void)supporteds; 199 return HDF_ERR_NOT_SUPPORT; 200 } 201 202 protected: 203 static constexpr uint32_t WAIT_TIME_INTERVAL = 10000; 204 sptr<IAllocator> allocator_; 205 sptr<IMapper> mapper_; 206 sptr<IRemoteObject::DeathRecipient> recipient_; 207 }; 208 using HdiDisplayBufferImpl = DisplayBufferHdiImpl<V1_0::IDisplayBuffer>; 209 } // namespace V1_0 210 } // namespace Buffer 211 } // namespace Display 212 } // namespace HDI 213 } // namespace OHOS 214 215 #endif // OHOS_HDI_DISPLAY_V1_0_DISPLAY_BUFFER_HDI_IMPL_H 216