1/* 2 * Copyright (c) 2021-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 "display_gralloc_client.h" 17#include "hdf_log.h" 18 19#define HDF_LOG_TAG HDI_DISP_CLIENT 20 21namespace OHOS { 22namespace HDI { 23namespace Display { 24namespace V1_0 { 25class GrallocDeathRecipient : public IRemoteObject::DeathRecipient { 26public: 27 GrallocDeathRecipient(AllocatorDeathCallback func, void *data) : deathCbFun_(func), data_(data) {}; 28 void OnRemoteDied(const wptr<IRemoteObject> &object) override 29 { 30 HDF_LOGI("%{public}s: allocator service is dead", __func__); 31 if (deathCbFun_ != nullptr) { 32 HDF_LOGI("%{public}s: notify the death event of allocator to RS", __func__); 33 deathCbFun_(data_); 34 } 35 } 36private: 37 AllocatorDeathCallback deathCbFun_; 38 void *data_; 39}; 40 41IDisplayGralloc *IDisplayGralloc::Get() 42{ 43 IDisplayGralloc *instance = nullptr; 44 instance = new DisplayGrallocClient(); 45 if (instance == nullptr) { 46 HDF_LOGE("%{public}s: Can't new a DisplayGrallocClient instance", __func__); 47 return nullptr; 48 } 49 HDF_LOGI("%{public}s: Get display gralloc client handle succ", __func__); 50 return instance; 51} 52 53DisplayGrallocClient::DisplayGrallocClient() : mapperAdapter_(std::make_shared<MapperAdapter>()) 54{ 55 allocatorProxy_ = IDisplayAllocator::Get("hdi_display_gralloc_service"); 56 if (allocatorProxy_ == nullptr) { 57 return; 58 } 59} 60 61int32_t DisplayGrallocClient::RegAllocatorDeathCallback(AllocatorDeathCallback func, void *data) 62{ 63 const sptr<IRemoteObject::DeathRecipient> recipient = new GrallocDeathRecipient(func, data); 64 if (allocatorProxy_ == nullptr) { 65 HDF_LOGE("%{public}s: allocatorProxy_ is null", __func__); 66 return DISPLAY_FAILURE; 67 } 68 bool ret = allocatorProxy_->AsObject()->AddDeathRecipient(recipient); 69 if (ret) { 70 HDF_LOGI("%{public}s: add alocator death notify success", __func__); 71 return DISPLAY_SUCCESS; 72 } else { 73 HDF_LOGE("%{public}s: add alocator death notify failed", __func__); 74 return DISPLAY_FAILURE; 75 } 76} 77 78int32_t DisplayGrallocClient::AllocMem(const AllocInfo &info, BufferHandle *&handle) const 79{ 80 if (allocatorProxy_ == nullptr) { 81 HDF_LOGE("%{public}s: allocatorProxy_ is null", __func__); 82 return DISPLAY_FAILURE; 83 } 84 auto ret = allocatorProxy_->AllocMem(info, handle); 85 return ret; 86} 87 88void DisplayGrallocClient::FreeMem(const BufferHandle &handle) const 89{ 90 mapperAdapter_->FreeBuffer(handle); 91} 92 93void* DisplayGrallocClient::Mmap(const BufferHandle &handle) const 94{ 95 void* data = nullptr; 96 int32_t ret = mapperAdapter_->MapBuffer(handle, data); 97 if (ret != DISPLAY_SUCCESS) { 98 FreeMem(handle); 99 HDF_LOGE("%{public}s: DisplayGrallocClient::Mmap, mapBuffer failed", __func__); 100 return nullptr; 101 } 102 return data; 103} 104 105int32_t DisplayGrallocClient::Unmap(const BufferHandle &handle) const 106{ 107 auto ret = mapperAdapter_->UnmapBuffer(handle); 108 if (ret != DISPLAY_SUCCESS) { 109 HDF_LOGE("%{public}s: failed, ret %{public}d", __func__, ret); 110 } 111 return ret; 112} 113 114int32_t DisplayGrallocClient::FlushCache(const BufferHandle &handle) const 115{ 116 auto ret = mapperAdapter_->FlushCache(handle); 117 if (ret != DISPLAY_SUCCESS) { 118 HDF_LOGE("%{public}s: failed, ret %{public}d", __func__, ret); 119 } 120 return ret; 121} 122 123int32_t DisplayGrallocClient::InvalidateCache(const BufferHandle &handle) const 124{ 125 auto ret = mapperAdapter_->InvalidateCache(handle); 126 if (ret != DISPLAY_SUCCESS) { 127 HDF_LOGI("%{public}s: failed, ret %{public}d", __func__, ret); 128 } 129 return ret; 130} 131 132void* DisplayGrallocClient::MmapCache(const BufferHandle &handle) const 133{ 134 (void)handle; 135 return nullptr; 136} 137 138int32_t DisplayGrallocClient::FlushMCache(const BufferHandle &handle) const 139{ 140 (void)handle; 141 return DISPLAY_NOT_SUPPORT; 142} 143 144int32_t DisplayGrallocClient::IsSupportedAlloc(const std::vector<VerifyAllocInfo> &infos, 145 std::vector<bool> &supporteds) const 146{ 147 (void)infos; 148 (void)supporteds; 149 return DISPLAY_NOT_SUPPORT; 150} 151} // namespace V1_0 152} // namespace Display 153} // namespace HDI 154} // namespace OHOS 155