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