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 "hdi_test_layer.h"
17 #include <unistd.h>
18 #include "hdi_test_device.h"
19 #include "v1_0/include/idisplay_buffer.h"
20 #include "v1_0/display_buffer_type.h"
21 #include "v1_1/display_composer_type.h"
22
23 namespace OHOS {
24 namespace HDI {
25 namespace Display {
26 namespace TEST {
27 using namespace OHOS::HDI::Display::Buffer::V1_0;
28 using namespace OHOS::HDI::Display::Composer::V1_1;
29
HdiGrallocBuffer(uint32_t seqNo,uint32_t w,uint32_t h,Composer::V1_0::PixelFormat fmt)30 HdiGrallocBuffer::HdiGrallocBuffer(uint32_t seqNo, uint32_t w, uint32_t h, Composer::V1_0::PixelFormat fmt)
31 {
32 std::shared_ptr<IDisplayBuffer> gralloc = HdiTestDevice::GetInstance().GetGrallocInterface();
33 AllocInfo info = { 0 };
34 info.width = w;
35 info.height = h;
36 info.usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE;
37 info.format = fmt;
38
39 BufferHandle* buffer = nullptr;
40 int ret = gralloc->AllocMem(info, buffer);
41 if (ret != DISPLAY_SUCCESS) {
42 DISPLAY_TEST_LOGE("can not alloc memory");
43 }
44 if (buffer != nullptr) {
45 void* vaddr = gralloc->Mmap(*buffer);
46 if (vaddr == nullptr) {
47 DISPLAY_TEST_LOGE("mmap failed");
48 }
49 buffer_ = buffer;
50 seqNo_ = seqNo;
51 }
52 }
53
~HdiGrallocBuffer()54 HdiGrallocBuffer::~HdiGrallocBuffer()
55 {
56 if (buffer_ != nullptr) {
57 std::shared_ptr<IDisplayBuffer> gralloc = HdiTestDevice::GetInstance().GetGrallocInterface();
58 if (buffer_->virAddr != nullptr) {
59 int ret = gralloc->Unmap(*buffer_);
60 if (ret != DISPLAY_SUCCESS) {
61 DISPLAY_TEST_LOGE("can not ummap buffer handle");
62 }
63 }
64 gralloc->FreeMem(*buffer_);
65 buffer_ = nullptr;
66 }
67 if (mReleaseFence != -1) {
68 close(mReleaseFence);
69 }
70 }
71
SetReleaseFence(int fd)72 void HdiGrallocBuffer::SetReleaseFence(int fd)
73 {
74 DISPLAY_TEST_LOGE("the fd is %{public}d", fd);
75 if (mReleaseFence != -1) {
76 close(mReleaseFence);
77 mReleaseFence = -1;
78 }
79 }
80
SetAcquirceFence(int fd)81 void HdiGrallocBuffer::SetAcquirceFence(int fd)
82 {
83 DISPLAY_TEST_LOGE("the fd is %{public}d", fd);
84 mAcquireFence = fd;
85 }
86
SetGraphicBuffer(std::function<int32_t (const BufferHandle *,uint32_t)> realFunc)87 int32_t HdiGrallocBuffer::SetGraphicBuffer(std::function<int32_t (const BufferHandle*, uint32_t)> realFunc)
88 {
89 DISPLAY_TEST_CHK_RETURN(buffer_ == nullptr, DISPLAY_FAILURE,
90 DISPLAY_TEST_LOGE("buffer handle is null"));
91 DISPLAY_TEST_CHK_RETURN(seqNo_ == UINT32_MAX, DISPLAY_FAILURE,
92 DISPLAY_TEST_LOGE("seqNo is invalid"));
93
94 int32_t ret = DISPLAY_SUCCESS;
95 if (cacheValid_ == false) {
96 ret = realFunc(buffer_, seqNo_);
97 cacheValid_ = (ret == DISPLAY_SUCCESS) ? true : false;
98 } else {
99 ret = realFunc(nullptr, seqNo_);
100 }
101 return ret;
102 }
103
AcquireBackBuffer()104 HdiGrallocBuffer* HdiTestLayer::AcquireBackBuffer()
105 {
106 if (!backBuffers_.empty()) {
107 if (currentBuffer_ != nullptr) {
108 frontBuffers_.emplace(std::move(currentBuffer_));
109 }
110 currentBuffer_ = std::move(backBuffers_.front());
111 backBuffers_.pop();
112 }
113 return currentBuffer_.get();
114 }
115
GetFrontBuffer() const116 HdiGrallocBuffer* HdiTestLayer::GetFrontBuffer() const
117 {
118 HdiGrallocBuffer* buffer = nullptr;
119 if (!frontBuffers_.empty()) {
120 buffer = frontBuffers_.front().get();
121 }
122 return buffer;
123 }
124
GetBackBuffer() const125 HdiGrallocBuffer* HdiTestLayer::GetBackBuffer() const
126 {
127 HdiGrallocBuffer* buffer = nullptr;
128 if (!backBuffers_.empty()) {
129 buffer = backBuffers_.front().get();
130 }
131 return buffer;
132 }
133
HdiTestLayer(LayerInfo & info,uint32_t id,uint32_t displayId)134 HdiTestLayer::HdiTestLayer(LayerInfo& info, uint32_t id, uint32_t displayId)
135 : id_(id), displayID_(displayId), layerBufferCount_(MAX_BUFFER_COUNT), layerInfo_(info)
136 {}
137
GenerateSeq()138 static uint32_t GenerateSeq()
139 {
140 static uint32_t originSeq = 0;
141 return originSeq++;
142 }
143
Init(uint32_t bufferCount)144 int32_t HdiTestLayer::Init(uint32_t bufferCount)
145 {
146 // init the font queue
147 DISPLAY_TEST_LOGE();
148 layerBufferCount_ = bufferCount;
149 for (uint32_t i = 0; i < layerBufferCount_; i++) {
150 std::unique_ptr<HdiGrallocBuffer> buffer =
151 std::make_unique<HdiGrallocBuffer>(GenerateSeq(),
152 layerInfo_.width, layerInfo_.height, layerInfo_.pixFormat);
153 DISPLAY_TEST_CHK_RETURN((buffer->Get() == nullptr), DISPLAY_FAILURE,
154 DISPLAY_TEST_LOGE("buffer handle is null"));
155 frontBuffers_.emplace(std::move(buffer));
156 }
157 displayRect_.w = layerInfo_.width;
158 displayRect_.h = layerInfo_.height;
159 cropRect_ = displayRect_;
160 return DISPLAY_SUCCESS;
161 }
162
163
SwapFrontToBackQ()164 int32_t HdiTestLayer::SwapFrontToBackQ()
165 {
166 DISPLAY_TEST_CHK_RETURN((frontBuffers_.empty()), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("the font buffer is empty"));
167 backBuffers_.emplace(std::move(frontBuffers_.front()));
168 frontBuffers_.pop();
169 return DISPLAY_SUCCESS;
170 }
171
SwapBackToFrontQ()172 int32_t HdiTestLayer::SwapBackToFrontQ()
173 {
174 DISPLAY_TEST_CHK_RETURN((backBuffers_.empty()), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("the font buffer is empty"));
175 frontBuffers_.emplace(std::move(backBuffers_.front()));
176 backBuffers_.pop();
177 return DISPLAY_SUCCESS;
178 }
179
SetLayerPosition(const IRect & rect)180 void HdiTestLayer::SetLayerPosition(const IRect& rect)
181 {
182 DISPLAY_TEST_LOGE("x : %{public}d y : %{public}d w : %{public}d h : %{public}d", rect.x, rect.y, rect.w, rect.h);
183 displayRect_ = rect;
184 }
185
SetLayerCrop(const IRect & rect)186 void HdiTestLayer::SetLayerCrop(const IRect& rect)
187 {
188 DISPLAY_TEST_LOGE("x : %{public}d y : %{public}d w : %{public}d h : %{public}d", rect.x, rect.y, rect.w, rect.h);
189 cropRect_ = rect;
190 }
191
PreparePresent()192 int32_t HdiTestLayer::PreparePresent()
193 {
194 int ret;
195 DISPLAY_TEST_LOGE();
196 ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerRegion(displayID_, id_, displayRect_);
197 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set display rect failed"));
198
199 ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerCrop(displayID_, id_, cropRect_);
200 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set display crop failed"));
201
202 ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerZorder(displayID_, id_, zorder_);
203 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set display zorder failed"));
204
205 ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerCompositionType(displayID_, id_, compType_);
206 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
207 DISPLAY_TEST_LOGE("set display composition type failed"));
208
209 ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerTransformMode(displayID_, id_, transform_);
210 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set transform mode failed"));
211
212 ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerAlpha(displayID_, id_, alpha_);
213 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set alpha failed"));
214
215 HdiGrallocBuffer* buffer = AcquireBackBuffer();
216 DISPLAY_TEST_CHK_RETURN((buffer == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("can not get back buffer"));
217
218 BufferHandle* handle = buffer->Get();
219 DISPLAY_TEST_CHK_RETURN((handle == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("BufferHandle is null"));
220
221 IRect tmp {0, 0, handle->width, handle->height};
222 std::vector<IRect> vRects;
223 vRects.push_back(tmp);
224 ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerVisibleRegion(displayID_, id_, vRects);
225 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
226 DISPLAY_TEST_LOGE("SetLayerVisibleRegion failed"));
227
228 ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerDirtyRegion(displayID_, id_, vRects);
229 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
230 DISPLAY_TEST_LOGE("SetLayerDirtyRegion failed"));
231
232 ret = buffer->SetGraphicBuffer([&](const BufferHandle* ptrBuffer, uint32_t seqNo) -> int32_t {
233 std::vector<uint32_t> deletingList;
234 int32_t result = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerBuffer(
235 displayID_, id_, ptrBuffer, seqNo, -1, deletingList);
236 DISPLAY_TEST_CHK_RETURN((result != DISPLAY_SUCCESS), DISPLAY_FAILURE,
237 DISPLAY_TEST_LOGE("set buffer handle failed"));
238 return DISPLAY_SUCCESS;
239 });
240 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), ret, DISPLAY_TEST_LOGE("set buffer handle failed"));
241
242 ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerBlendType(displayID_, id_, blendType_);
243 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set blend type failed"));
244 return DISPLAY_SUCCESS;
245 }
246
SetZorder(uint32_t zorder)247 void HdiTestLayer::SetZorder(uint32_t zorder)
248 {
249 DISPLAY_TEST_LOGE("the zorder is %{public}u", zorder);
250 zorder_ = zorder;
251 }
252
SetCompType(Composer::V1_0::CompositionType type)253 void HdiTestLayer::SetCompType(Composer::V1_0::CompositionType type)
254 {
255 DISPLAY_TEST_LOGE("layer id %{public}u ,the type is : %{public}d", id_, type);
256 compType_ = type;
257 }
258
SetTransform(TransformType transform)259 void HdiTestLayer::SetTransform(TransformType transform)
260 {
261 transform_ = transform;
262 }
263
SetAlpha(LayerAlpha alpha)264 void HdiTestLayer::SetAlpha(LayerAlpha alpha)
265 {
266 DISPLAY_TEST_LOGE();
267 alpha_ = alpha;
268 }
269
SetBlendType(BlendType type)270 void HdiTestLayer::SetBlendType(BlendType type)
271 {
272 DISPLAY_TEST_LOGE("type %{public}d", type);
273 blendType_ = type;
274 }
275
SetReleaseFence(int fd)276 void HdiTestLayer::SetReleaseFence(int fd)
277 {
278 DISPLAY_TEST_LOGE("layer id %{public}u , fd %{public}d", id_, fd);
279 if (currentBuffer_ != nullptr) {
280 currentBuffer_->SetReleaseFence(fd);
281 }
282 }
283
GetLayerBuffercount() const284 uint32_t HdiTestLayer::GetLayerBuffercount() const
285 {
286 return layerBufferCount_;
287 }
288
~HdiTestLayer()289 HdiTestLayer::~HdiTestLayer() {}
290 } // OHOS
291 } // HDI
292 } // Display
293 } // TEST
294