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