1 /*
2 * Copyright (c) 2022 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 #include "display_mock_surface.h"
16 #include "libsync.h"
17 #include "display_common.h"
18 #include "display_gralloc.h"
19 namespace OHOS {
AllocBuffer(AllocInfo & info)20 bool DisplayMockSurface::AllocBuffer(AllocInfo &info)
21 {
22 int32_t ret;
23 BufferHandle *bufferHandle = nullptr;
24 DISPLAY_LOGD();
25 ret = grallocFuncs_->AllocMem(&info, &bufferHandle);
26 DISPLAY_CHK_RETURN(((ret != DISPLAY_SUCCESS) || (bufferHandle == nullptr)), false,
27 DISPLAY_LOGE("can not alloc buffer"));
28 auto surfaceBuffer = HardwareBuffer::Create(*bufferHandle);
29 hardwareBufferQueue_.push(surfaceBuffer);
30 // the HardwareBuffer will hold the bufferhandle, now we can free the bufferhandle
31 grallocFuncs_->FreeMem(bufferHandle);
32 return true;
33 }
34
Init(uint32_t width,uint32_t height)35 bool DisplayMockSurface::Init(uint32_t width, uint32_t height)
36 {
37 int32_t ret;
38 DISPLAY_LOGD();
39 ret = GrallocInitialize(&grallocFuncs_);
40 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("can not get gralloc function"));
41 for (int i = 0; i < BUFFER_NUM; i++) {
42 AllocInfo info;
43 info.format = PIXEL_FMT_RGBA_8888;
44 info.width = width;
45 info.height = height;
46 info.usage = HBM_USE_MEM_FB | HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE;
47 AllocBuffer(info);
48 }
49 width_ = width;
50 height_ = height;
51 return true;
52 }
53
IsConsumer() const54 bool DisplayMockSurface::IsConsumer() const
55 {
56 DISPLAY_LOGD();
57 return false;
58 }
59
GetProducer() const60 sptr<IBufferProducer> DisplayMockSurface::GetProducer() const
61 {
62 DISPLAY_LOGD();
63 return nullptr;
64 }
65
RequestBuffer(sptr<SurfaceBuffer> & buffer,int32_t & fence,BufferRequestConfig & config)66 GSError DisplayMockSurface::RequestBuffer(sptr<SurfaceBuffer> &buffer, int32_t &fence, BufferRequestConfig &config)
67 {
68 DISPLAY_LOGD();
69 if (hardwareBufferQueue_.empty()) {
70 DISPLAY_LOGE("has no buffer");
71 return GSERROR_NO_BUFFER;
72 }
73 auto surfaceBuffer = hardwareBufferQueue_.front();
74 if (surfaceBuffer == nullptr) {
75 DISPLAY_LOGE("the buffer is nullptr");
76 return GSERROR_NO_BUFFER;
77 }
78 HardwareBuffer *hardwareBuffer = static_cast<HardwareBuffer *>(surfaceBuffer.GetRefPtr());
79 fence = hardwareBuffer->GetReleaseFence();
80 // todo: now the ddk kernel can not support acquire fence, need fix it latter
81 if (fence >= 0) {
82 sync_wait(fence, FENCE_TIMEOUT);
83 close(fence);
84 fence = -1;
85 }
86 buffer = surfaceBuffer;
87 hardwareBufferQueue_.pop();
88 DISPLAY_LOGD("success");
89 return GSERROR_OK;
90 }
91
CancelBuffer(sptr<SurfaceBuffer> & buffer)92 GSError DisplayMockSurface::CancelBuffer(sptr<SurfaceBuffer> &buffer)
93 {
94 DISPLAY_LOGD();
95 return GSERROR_OK;
96 }
97
FlushBuffer(sptr<SurfaceBuffer> & buffer,int32_t fence,BufferFlushConfig & config)98 GSError DisplayMockSurface::FlushBuffer(sptr<SurfaceBuffer> &buffer, int32_t fence, BufferFlushConfig &config)
99 {
100 DISPLAY_LOGD();
101 curentPresentBuffer_ = buffer;
102 // todo fence
103 if (fence >= 0) {
104 close(fence);
105 fence = -1;
106 }
107 HardwareBuffer *hardwareBuffer = static_cast<HardwareBuffer *>(buffer.GetRefPtr());
108 DISPLAY_CHK_RETURN((hardwareBuffer == nullptr), GSERROR_NO_BUFFER, DISPLAY_LOGE("can not get the ptr"));
109
110 return GSERROR_OK;
111 }
112
AcquireBuffer(sptr<SurfaceBuffer> & buffer,int32_t & fence,int64_t & timestamp,Rect & damage)113 GSError DisplayMockSurface::AcquireBuffer(sptr<SurfaceBuffer> &buffer, int32_t &fence, int64_t ×tamp, Rect &damage)
114 {
115 DISPLAY_LOGD();
116 buffer = curentPresentBuffer_;
117 fence = -1;
118 return GSERROR_OK;
119 }
120
AcquireBufferHandle(HDI::DISPLAY::HdiLayer & layer)121 void DisplayMockSurface::AcquireBufferHandle(HDI::DISPLAY::HdiLayer &layer)
122 {
123 DISPLAY_LOGD();
124 if (curentPresentBuffer_ == nullptr) {
125 DISPLAY_LOGE("cureent present buffer is nullptr");
126 return;
127 }
128 layer.SetLayerBuffer(curentPresentBuffer_->GetBufferHandle(), -1);
129 }
130
SetReleaseFence(int32_t fence)131 void DisplayMockSurface::SetReleaseFence(int32_t fence)
132 {
133 DISPLAY_LOGD("release fence %{public}d", fence);
134 if (curentPresentBuffer_ == nullptr) {
135 DISPLAY_LOGW("cureent present buffer is nullptr");
136 return;
137 }
138 HardwareBuffer *buffer = reinterpret_cast<HardwareBuffer *>(curentPresentBuffer_.GetRefPtr());
139 buffer->SetReleaseFence(fence);
140 hardwareBufferQueue_.push(curentPresentBuffer_);
141 curentPresentBuffer_ = nullptr;
142 }
143
ReleaseBuffer(sptr<SurfaceBuffer> & buffer,int32_t fence)144 GSError DisplayMockSurface::ReleaseBuffer(sptr<SurfaceBuffer> &buffer, int32_t fence)
145 {
146 DISPLAY_LOGD();
147 return GSERROR_OK;
148 }
149
AttachBuffer(sptr<SurfaceBuffer> & buffer)150 GSError DisplayMockSurface::AttachBuffer(sptr<SurfaceBuffer> &buffer)
151 {
152 DISPLAY_LOGD();
153 return GSERROR_OK;
154 }
DetachBuffer(sptr<SurfaceBuffer> & buffer)155 GSError DisplayMockSurface::DetachBuffer(sptr<SurfaceBuffer> &buffer)
156 {
157 DISPLAY_LOGD();
158 return GSERROR_OK;
159 }
160
GetQueueSize()161 uint32_t DisplayMockSurface::GetQueueSize()
162 {
163 DISPLAY_LOGD();
164 return BUFFER_NUM;
165 }
166
SetQueueSize(uint32_t queueSize)167 GSError DisplayMockSurface::SetQueueSize(uint32_t queueSize)
168 {
169 DISPLAY_LOGD();
170 return GSERROR_OK;
171 }
172
SetDefaultWidthAndHeight(int32_t width,int32_t height)173 GSError DisplayMockSurface::SetDefaultWidthAndHeight(int32_t width, int32_t height)
174 {
175 DISPLAY_LOGD();
176 return GSERROR_OK;
177 }
178
GetDefaultWidth()179 int32_t DisplayMockSurface::GetDefaultWidth()
180 {
181 DISPLAY_LOGD();
182 return width_;
183 }
184
GetDefaultHeight()185 int32_t DisplayMockSurface::GetDefaultHeight()
186 {
187 DISPLAY_LOGD();
188 return height_;
189 }
190
SetDefaultUsage(uint32_t usage)191 GSError DisplayMockSurface::SetDefaultUsage(uint32_t usage)
192 {
193 DISPLAY_LOGD();
194 return GSERROR_OK;
195 }
GetDefaultUsage()196 uint32_t DisplayMockSurface::GetDefaultUsage()
197 {
198 DISPLAY_LOGD();
199 return GSERROR_OK;
200 }
201
SetUserData(const std::string & key,const std::string & val)202 GSError DisplayMockSurface::SetUserData(const std::string &key, const std::string &val)
203 {
204 DISPLAY_LOGD();
205 return GSERROR_OK;
206 }
GetUserData(const std::string & key)207 std::string DisplayMockSurface::GetUserData(const std::string &key)
208 {
209 DISPLAY_LOGD();
210 return "";
211 }
212
GetName(std::string & name)213 GSError DisplayMockSurface::GetName(std::string &name)
214 {
215 DISPLAY_LOGD();
216 return GSERROR_OK;
217 }
218
RegisterConsumerListener(sptr<IBufferConsumerListener> & listener)219 GSError DisplayMockSurface::RegisterConsumerListener(sptr<IBufferConsumerListener> &listener)
220 {
221 DISPLAY_LOGD();
222 return GSERROR_OK;
223 }
RegisterConsumerListener(IBufferConsumerListenerClazz * listener)224 GSError DisplayMockSurface::RegisterConsumerListener(IBufferConsumerListenerClazz *listener)
225 {
226 DISPLAY_LOGD();
227 return GSERROR_OK;
228 }
RegisterReleaseListener(OnReleaseFunc func)229 GSError DisplayMockSurface::RegisterReleaseListener(OnReleaseFunc func)
230 {
231 DISPLAY_LOGD();
232 return GSERROR_OK;
233 }
UnregisterConsumerListener()234 GSError DisplayMockSurface::UnregisterConsumerListener()
235 {
236 DISPLAY_LOGD();
237 return GSERROR_OK;
238 }
239
GetUniqueId() const240 uint64_t DisplayMockSurface::GetUniqueId() const
241 {
242 DISPLAY_LOGD();
243 return 0;
244 }
245
Dump(std::string & result) const246 void DisplayMockSurface::Dump(std::string &result) const
247 {
248 DISPLAY_LOGD();
249 }
250
CleanCache()251 GSError DisplayMockSurface::CleanCache()
252 {
253 DISPLAY_LOGD();
254 return GSERROR_OK;
255 }
256 }
257