1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include "allocator_proxy.h" 17094332d3Sopenharmony_ci#include <message_parcel.h> 18094332d3Sopenharmony_ci#include "buffer_handle_parcel.h" 19094332d3Sopenharmony_ci#include "iremote_object.h" 20094332d3Sopenharmony_ci#include "iservmgr_hdi.h" 21094332d3Sopenharmony_ci#include "parcel_utils.h" 22094332d3Sopenharmony_ci#include "unistd.h" 23094332d3Sopenharmony_ci#include "refbase.h" 24094332d3Sopenharmony_ci 25094332d3Sopenharmony_ci#define HDF_LOG_TAG HDI_DISP_PROXY 26094332d3Sopenharmony_ci 27094332d3Sopenharmony_cinamespace OHOS { 28094332d3Sopenharmony_cinamespace HDI { 29094332d3Sopenharmony_cinamespace Display { 30094332d3Sopenharmony_cinamespace V1_0 { 31094332d3Sopenharmony_cisptr<IDisplayAllocator> IDisplayAllocator::Get(const char *serviceName) 32094332d3Sopenharmony_ci{ 33094332d3Sopenharmony_ci constexpr uint32_t sleepTime = 10000; 34094332d3Sopenharmony_ci constexpr uint32_t waitSvcTimeout = 1000; 35094332d3Sopenharmony_ci constexpr uint32_t waitSvcMgrTimeout = 10; 36094332d3Sopenharmony_ci constexpr uint32_t printPeriod = 100; 37094332d3Sopenharmony_ci using namespace OHOS::HDI::ServiceManager::V1_0; 38094332d3Sopenharmony_ci 39094332d3Sopenharmony_ci static sptr<IServiceManager> servMgr = IServiceManager::Get(); 40094332d3Sopenharmony_ci uint32_t cnt = 0; 41094332d3Sopenharmony_ci while (servMgr == nullptr) { 42094332d3Sopenharmony_ci if (cnt > waitSvcMgrTimeout) { 43094332d3Sopenharmony_ci HDF_LOGE("%{public}s: wait IServiceManager timeout cnt:%{public}u", __func__, cnt); 44094332d3Sopenharmony_ci return nullptr; 45094332d3Sopenharmony_ci } 46094332d3Sopenharmony_ci usleep(sleepTime); // 10 ms 47094332d3Sopenharmony_ci servMgr = IServiceManager::Get(); 48094332d3Sopenharmony_ci cnt++; 49094332d3Sopenharmony_ci HDF_LOGI("%{public}s: IServiceManager cnt:%{public}u", __func__, cnt); 50094332d3Sopenharmony_ci } 51094332d3Sopenharmony_ci HDF_LOGI("%{public}s: get IServiceManager success cnt:%{public}u", __func__, cnt); 52094332d3Sopenharmony_ci 53094332d3Sopenharmony_ci cnt = 0; 54094332d3Sopenharmony_ci sptr<IRemoteObject> remote = servMgr->GetService(serviceName); 55094332d3Sopenharmony_ci while (remote == nullptr) { 56094332d3Sopenharmony_ci if (cnt > waitSvcTimeout) { 57094332d3Sopenharmony_ci HDF_LOGE("%{public}s: wait service:%{public}s timeout cnt:%{public}u", __func__, serviceName, cnt); 58094332d3Sopenharmony_ci return nullptr; 59094332d3Sopenharmony_ci } 60094332d3Sopenharmony_ci usleep(sleepTime); // 10 ms 61094332d3Sopenharmony_ci remote = servMgr->GetService(serviceName); 62094332d3Sopenharmony_ci if (((cnt++) % printPeriod) == 0) { 63094332d3Sopenharmony_ci HDF_LOGI("%{public}s: get service:%{public}s cnt:%{public}u", __func__, serviceName, cnt); 64094332d3Sopenharmony_ci } 65094332d3Sopenharmony_ci } 66094332d3Sopenharmony_ci HDF_LOGI("%{public}s: get service:%{public}s success cnt:%{public}u", __func__, serviceName, cnt); 67094332d3Sopenharmony_ci 68094332d3Sopenharmony_ci sptr<AllocatorProxy> hostSptr = iface_cast<AllocatorProxy>(remote); 69094332d3Sopenharmony_ci if (hostSptr == nullptr) { 70094332d3Sopenharmony_ci HDF_LOGE("%{public}s: IServiceManager GetService null ptr", __func__); 71094332d3Sopenharmony_ci return nullptr; 72094332d3Sopenharmony_ci } 73094332d3Sopenharmony_ci HDF_LOGE("%{public}s: GetService %{public}s ok", __func__, serviceName); 74094332d3Sopenharmony_ci return hostSptr; 75094332d3Sopenharmony_ci} 76094332d3Sopenharmony_ci 77094332d3Sopenharmony_ciint32_t AllocatorProxy::AllocMem(const AllocInfo &info, BufferHandle *&handle) 78094332d3Sopenharmony_ci{ 79094332d3Sopenharmony_ci MessageParcel data; 80094332d3Sopenharmony_ci MessageParcel reply; 81094332d3Sopenharmony_ci MessageOption option; 82094332d3Sopenharmony_ci if (!data.WriteInterfaceToken(AllocatorProxy::GetDescriptor())) { 83094332d3Sopenharmony_ci return HDF_FAILURE; 84094332d3Sopenharmony_ci } 85094332d3Sopenharmony_ci auto ret = ParcelUtils::PackAllocInfo(data, &info); 86094332d3Sopenharmony_ci if (ret != DISPLAY_SUCCESS) { 87094332d3Sopenharmony_ci return ret; 88094332d3Sopenharmony_ci } 89094332d3Sopenharmony_ci int32_t retCode = Remote()->SendRequest(CMD_REMOTE_ALLOCATOR_ALLOCMEM, data, reply, option); 90094332d3Sopenharmony_ci if (retCode != HDF_SUCCESS) { 91094332d3Sopenharmony_ci HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}x", __func__, retCode); 92094332d3Sopenharmony_ci return retCode; 93094332d3Sopenharmony_ci } 94094332d3Sopenharmony_ci 95094332d3Sopenharmony_ci retCode = reply.ReadInt32(); 96094332d3Sopenharmony_ci if (retCode != HDF_SUCCESS) { 97094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Read return code failed, error code is %{public}x", __func__, retCode); 98094332d3Sopenharmony_ci return retCode; 99094332d3Sopenharmony_ci } 100094332d3Sopenharmony_ci 101094332d3Sopenharmony_ci auto retHandle = ReadBufferHandle(reply); 102094332d3Sopenharmony_ci if (retHandle != nullptr) { 103094332d3Sopenharmony_ci handle = retHandle; 104094332d3Sopenharmony_ci retCode = DISPLAY_SUCCESS; 105094332d3Sopenharmony_ci } else { 106094332d3Sopenharmony_ci retCode = DISPLAY_NULL_PTR; 107094332d3Sopenharmony_ci } 108094332d3Sopenharmony_ci return retCode; 109094332d3Sopenharmony_ci} 110094332d3Sopenharmony_ci} // namespace V1_0 111094332d3Sopenharmony_ci} // namespace Display 112094332d3Sopenharmony_ci} // namespace HDI 113094332d3Sopenharmony_ci} // namespace OHOS 114