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_device_test.h"
17 #include <chrono>
18 #include <cinttypes>
19 #include <algorithm>
20 #include "v1_0/include/idisplay_composer_interface.h"
21 #include "v1_0/display_composer_type.h"
22 #include "v1_0/display_buffer_type.h"
23 #include "display_test.h"
24 #include "display_test_utils.h"
25 #include "hdi_composition_check.h"
26 #include "hdi_test_device.h"
27 #include "hdi_test_device_common.h"
28 #include "hdi_test_display.h"
29 #include "hdi_test_render_utils.h"
30
31 using namespace OHOS::HDI::Display::Buffer::V1_0;
32 using namespace OHOS::HDI::Display::Composer::V1_0;
33 using namespace OHOS::HDI::Display::TEST;
34
35 const int SLEEP_CONT_100 = 100;
36 const int SLEEP_CONT_2000 = 2000;
37
38 static const std::vector<std::vector<LayerSettings>> TEST_SINGLE_LAYER = {
39 // one layer display test
40 {
41 {
42 .rectRatio = { 0, 0, 1.0f, 0.125f },
43 .color = RED
44 },
45 },
46 {
47 {
48 .rectRatio = { 0, 0.875f, 1.0f, 0.125f },
49 .color = GREEN
50 },
51 },
52 {
53 {
54 .rectRatio = { 0, 0.0, 1.0f, 1.0f },
55 .color = YELLOW
56 },
57 },
58 };
59
60 static const std::vector<std::vector<LayerSettings>> TEST_MULTILAYER = {
61
62 // three layer displayrect test
63 {
64 {
65 .rectRatio = { 0, 0, 1.0f, 1.0f },
66 .color = RED
67 },
68 {
69 .rectRatio = { 0, 0, 1.0f, 0.125f },
70 .color = GREEN
71 },
72 {
73 .rectRatio = { 0, 0.875f, 1.0f, 0.125f },
74 .color = YELLOW
75 },
76 }
77
78 };
79
80 static const std::vector<std::vector<LayerSettings>> TEST_SCALE = {
81 // scale layer test
82 {
83 {
84 .rectRatio = { 0, 0, 1.0f, 1.0f },
85 .color = RED,
86 .bufferRatio = { 1, 1 }
87 },
88 },
89 {
90 {
91 .rectRatio = { 0, 0, 1.0f, 0.125f },
92 .color = GREEN,
93 .bufferRatio = { 1, 1 }
94 },
95 },
96 {
97 {
98 .rectRatio = { 0, 0.875f, 1.0f, 0.125f },
99 .color = YELLOW,
100 .bufferRatio = { 1, 1 }
101 },
102 }
103 };
104
105 static const std::vector<std::vector<LayerSettings>> TEST_VIDEO = {
106 // video layer test
107 {
108 {
109 .rectRatio = { 0, 0, 1.0f, 0.125f },
110 .color = GREEN,
111 .compositionType = COMPOSITION_VIDEO
112 },
113 },
114
115 {
116 {
117 .rectRatio = { 0, 0, 1.0f, 1.0f },
118 .color = RED },
119 {
120 .rectRatio = { 0, 0, 1.0f, 1.0f },
121 .color = RED,
122 .compositionType = COMPOSITION_VIDEO
123 },
124 },
125 {
126 {
127 .rectRatio = { 0, 0, 1.0f, 1.0f },
128 .color = RED },
129 {
130 .rectRatio = { 0, 0, 1.0f, 0.125f },
131 .color = GREEN,
132 .compositionType = COMPOSITION_VIDEO
133 },
134 },
135 {
136 {
137 .rectRatio = { 0, 0, 1.0f, 1.0f },
138 .color = RED },
139 {
140 .rectRatio = { 0, 0.875f, 1.0f, 0.125f },
141 .color = YELLOW,
142 .compositionType = COMPOSITION_VIDEO
143 },
144 }
145 };
146
147 static const std::vector<std::vector<LayerSettings>> TEST_ALPHA = {
148 // alpha layer test
149 {
150 {
151 .rectRatio = { 0, 0, 1.0f, 1.0f },
152 .color = RED,
153 .alpha = 0xFF
154 },
155 },
156 {
157 {
158 .rectRatio = { 0, 0, 1.0f, 1.0f },
159 .color = RED,
160 .alpha = 0xFF
161 },
162 },
163 {
164 {
165 .rectRatio = { 0, 0, 1.0f, 1.0f },
166 .color = RED,
167 .alpha = 0xFF
168 },
169 },
170 // RED float will override, must use green now
171 {
172 {
173 .rectRatio = { 0, 0, 1.0f, 1.0f },
174 .color = GREEN,
175 .alpha = 0xFF,
176 .blendType = BLEND_SRCOVER
177 },
178 },
179 {
180 {
181 .rectRatio = { 0, 0, 1.0f, 1.0f },
182 .color = GREEN,
183 .alpha = 0xFF,
184 .blendType = BLEND_SRCOVER
185 },
186 },
187 {
188 {
189 .rectRatio = { 0, 0, 1.0f, 1.0f },
190 .color = GREEN,
191 .alpha = 0xFF,
192 .blendType = BLEND_SRCOVER
193 },
194 },
195 };
196
197 static const std::vector<LayerSettings> TEST_ROTATE = {
198 {
199 .rectRatio = { 0, 0, 1.0f, 1.0f },
200 .color = RED,
201 .rotate = ROTATE_NONE
202 },
203 {
204 .rectRatio = { 0, 0, 1.0f, 1.0f },
205 .color = RED,
206 .rotate = ROTATE_90
207 },
208 {
209 .rectRatio = { 0, 0, 1.0f, 1.0f },
210 .color = RED,
211 .rotate = ROTATE_180
212 },
213 {
214 .rectRatio = { 0, 0, 1.0f, 1.0f },
215 .color = RED,
216 .rotate = ROTATE_270
217 },
218 };
219
GetFirstDisplay()220 static inline std::shared_ptr<HdiTestDisplay> GetFirstDisplay()
221 {
222 return HdiTestDevice::GetInstance().GetFirstDisplay();
223 }
224
CheckComposition(std::vector<LayerSettings> & layers,BufferHandle * clientBuffer,uint32_t checkType=HdiCompositionCheck::CHECK_VERTEX)225 static int32_t CheckComposition(std::vector<LayerSettings> &layers, BufferHandle* clientBuffer,
226 uint32_t checkType = HdiCompositionCheck::CHECK_VERTEX)
227 {
228 DISPLAY_TEST_CHK_RETURN((clientBuffer == nullptr), DISPLAY_NULL_PTR, DISPLAY_TEST_LOGE("client buffer is nullptr"));
229 return HdiCompositionCheck::GetInstance().Check(layers, *clientBuffer, checkType);
230 }
231
CreateTestLayer(LayerSettings setting,uint32_t zorder)232 static std::shared_ptr<HdiTestLayer> CreateTestLayer(LayerSettings setting, uint32_t zorder)
233 {
234 int ret;
235 HdiTestDevice::GetInstance();
236 DISPLAY_TEST_LOGD("color 0x%x", setting.color);
237 std::shared_ptr<HdiTestDisplay> display = HdiTestDevice::GetInstance().GetFirstDisplay();
238 DISPLAY_TEST_CHK_RETURN((display == nullptr), nullptr, DISPLAY_TEST_LOGE("can not get display"));
239
240 std::shared_ptr<HdiTestLayer> layer = display->CreateHdiTestLayer(setting.bufferSize.w, setting.bufferSize.h);
241 DISPLAY_TEST_CHK_RETURN((layer == nullptr), nullptr, DISPLAY_TEST_LOGE("can not create hdi test layer"));
242
243 layer->SetLayerPosition(setting.displayRect);
244
245 layer->SetCompType(setting.compositionType);
246
247 if ((setting.alpha >= 0) && (setting.alpha <= 0xff)) { // alpha rang 0x00 ~ 0xff
248 LayerAlpha alpha = { 0 };
249 alpha.gAlpha = setting.alpha;
250 alpha.enGlobalAlpha = true;
251 layer->SetAlpha(alpha);
252 }
253 HdiGrallocBuffer* handle = layer->GetFrontBuffer();
254 DISPLAY_TEST_CHK_RETURN((handle == nullptr), nullptr, DISPLAY_TEST_LOGE("can not get front buffer"));
255 ClearColor(*(handle->Get()), setting.color);
256 ret = layer->SwapFrontToBackQ();
257 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), nullptr, DISPLAY_TEST_LOGE("SwapFrontToBackQ failed"));
258 layer->SetZorder(zorder);
259 layer->SetBlendType(setting.blendType);
260 layer->SetTransform(setting.rotate);
261 return layer;
262 }
263
PrepareAndPrensent()264 static int PrepareAndPrensent()
265 {
266 int ret;
267 DISPLAY_TEST_LOGD();
268 std::shared_ptr<HdiTestDisplay> display = HdiTestDevice::GetInstance().GetFirstDisplay();
269 DISPLAY_TEST_CHK_RETURN((display == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("can not get display"));
270
271 ret = display->PrepareDisplayLayers();
272 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
273 DISPLAY_TEST_LOGE("PrepareDisplayLayers failed"));
274
275 ret = display->Commit();
276 DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("Commit failed"));
277 return DISPLAY_SUCCESS;
278 }
279
TestVBlankCallback(unsigned int sequence,uint64_t ns,void * data)280 static void TestVBlankCallback(unsigned int sequence, uint64_t ns, void* data)
281 {
282 static uint64_t lastns;
283 DISPLAY_TEST_LOGD("seq %{public}d ns %" PRId64 " duration %" PRId64 " ns", sequence, ns, (ns - lastns));
284 lastns = ns;
285 VblankCtr::GetInstance().NotifyVblank(sequence, ns, data);
286 }
287
AdjustLayerSettings(std::vector<LayerSettings> & settings,uint32_t w,uint32_t h)288 static void AdjustLayerSettings(std::vector<LayerSettings> &settings, uint32_t w, uint32_t h)
289 {
290 DISPLAY_TEST_LOGD();
291 for (uint32_t i = 0; i < settings.size(); i++) {
292 LayerSettings& setting = settings[i];
293 DISPLAY_TEST_LOGD(" ratio w: %f ratio h: %f", setting.rectRatio.w, setting.rectRatio.h);
294 if ((setting.rectRatio.w > 0.0f) && (setting.rectRatio.h > 0.0f)) {
295 setting.displayRect.h = static_cast<uint32_t>(setting.rectRatio.h * h);
296 setting.displayRect.w = static_cast<uint32_t>(setting.rectRatio.w * w);
297 setting.displayRect.x = static_cast<uint32_t>(setting.rectRatio.x * w);
298 setting.displayRect.y = static_cast<uint32_t>(setting.rectRatio.y * h);
299 DISPLAY_TEST_LOGD("display rect adust form %f %f %f %f to %{public}d %{public}d %{public}d %{public}d ",
300 setting.rectRatio.x, setting.rectRatio.y, setting.rectRatio.w, setting.rectRatio.h,
301 setting.displayRect.x, setting.displayRect.y, setting.displayRect.w, setting.displayRect.h);
302 }
303
304 if ((setting.bufferRatio.h > 0.0f) || (setting.bufferRatio.w > 0.0f)) {
305 setting.bufferSize.h = static_cast<uint32_t>(setting.bufferRatio.h * h);
306 setting.bufferSize.w = static_cast<uint32_t>(setting.bufferRatio.w * w);
307 DISPLAY_TEST_LOGD("buffer size adjust for %f %f to %{public}d %{public}d",
308 setting.bufferRatio.w, setting.bufferRatio.h, setting.bufferSize.w, setting.bufferSize.h);
309 }
310
311 if ((setting.bufferSize.w == 0) || (setting.bufferSize.h == 0)) {
312 DISPLAY_TEST_LOGD("buffer size adjust for %{public}d %{public}d to %{public}d %{public}d",
313 setting.bufferSize.w, setting.bufferSize.h, setting.displayRect.w, setting.displayRect.h);
314
315 setting.bufferSize.w = setting.displayRect.w;
316 setting.bufferSize.h = setting.displayRect.h;
317 }
318 }
319 }
320
CreateLayers(std::vector<LayerSettings> & settings)321 static std::vector<std::shared_ptr<HdiTestLayer>> CreateLayers(std::vector<LayerSettings> &settings)
322 {
323 DISPLAY_TEST_LOGD("settings %{public}zd", settings.size());
324 std::vector<std::shared_ptr<HdiTestLayer>> layers;
325 DisplayModeInfo mode = GetFirstDisplay()->GetCurrentMode();
326 AdjustLayerSettings(settings, mode.width, mode.height);
327 for (uint32_t i = 0; i < settings.size(); i++) {
328 LayerSettings setting = settings[i];
329
330 auto layer = CreateTestLayer(setting, i);
331 layers.push_back(layer);
332 }
333
334 return layers;
335 }
336
PresentAndCheck(std::vector<LayerSettings> & layerSettings,uint32_t checkType=HdiCompositionCheck::CHECK_VERTEX)337 static inline void PresentAndCheck(std::vector<LayerSettings> &layerSettings,
338 uint32_t checkType = HdiCompositionCheck::CHECK_VERTEX)
339 {
340 int ret = PrepareAndPrensent();
341 ASSERT_TRUE((ret == DISPLAY_SUCCESS));
342 if ((GetFirstDisplay()->SnapShot()) != nullptr) {
343 HdiTestDevice::GetInstance().GetGrallocInterface()->InvalidateCache(*(GetFirstDisplay()->SnapShot()));
344 ret = CheckComposition(layerSettings, GetFirstDisplay()->SnapShot(), checkType);
345 ASSERT_TRUE((ret == DISPLAY_SUCCESS));
346 }
347 }
348
TearDown()349 void LayerRotateTest::TearDown()
350 {
351 HdiTestDevice::GetInstance().Clear();
352 }
353
TearDownTestCase()354 void LayerRotateTest::TearDownTestCase()
355 {
356 HdiTestDevice::GetInstance().GetFirstDisplay()->ResetClientLayer();
357 }
358
SetUpTestCase()359 void DeviceTest::SetUpTestCase()
360 {
361 int ret = HdiTestDevice::GetInstance().InitDevice();
362 ASSERT_TRUE(ret == DISPLAY_SUCCESS);
363 }
364
TearDown()365 void DeviceTest::TearDown()
366 {
367 HdiTestDevice::GetInstance().Clear();
368 }
369
TearDown()370 void DeviceLayerDisplay::TearDown()
371 {
372 HdiTestDevice::GetInstance().Clear();
373 }
374
NotifyVblank(unsigned int sequence,uint64_t ns,const void * data)375 void VblankCtr::NotifyVblank(unsigned int sequence, uint64_t ns, const void* data)
376 {
377 DISPLAY_TEST_LOGD();
378 if (data != nullptr) {
379 DISPLAY_TEST_LOGD("sequence = %{public}u, ns = %" PRIu64 "", sequence, ns);
380 }
381 std::unique_lock<std::mutex> lg(vblankMutex_);
382 hasVblank_ = true;
383 vblankCondition_.notify_one();
384 DISPLAY_TEST_LOGD();
385 }
386
~VblankCtr()387 VblankCtr::~VblankCtr() {}
388
WaitVblank(uint32_t ms)389 int32_t VblankCtr::WaitVblank(uint32_t ms)
390 {
391 bool ret = false;
392 DISPLAY_TEST_LOGD();
393 std::unique_lock<std::mutex> lck(vblankMutex_);
394 hasVblank_ = false; // must wait next vblank
395 ret = vblankCondition_.wait_for(lck, std::chrono::milliseconds(ms), [=] { return hasVblank_; });
396 DISPLAY_TEST_LOGD();
397 if (!ret) {
398 return DISPLAY_FAILURE;
399 }
400 return DISPLAY_SUCCESS;
401 }
402
TEST_P(DeviceLayerDisplay,LayerDisplay)403 TEST_P(DeviceLayerDisplay, LayerDisplay)
404 {
405 std::vector<LayerSettings> layerSettings = GetParam();
406 CreateLayers(layerSettings);
407 PresentAndCheck(layerSettings);
408 if (TestParemeter::GetInstance().mTestSleep > 0) {
409 sleep(TestParemeter::GetInstance().mTestSleep);
410 }
411 }
412
TEST_F(DeviceTest,zorder)413 TEST_F(DeviceTest, zorder)
414 {
415 std::vector<LayerSettings> settings = {
416 {
417 .rectRatio = { 0, 0, 1.0f, 1.0f },
418 .color = RED },
419 {
420 .rectRatio = { 0, 0, 1.0f, 0.125f },
421 .color = GREEN },
422 {
423 .rectRatio = { 0, 0.875f, 1.0f, 0.125f },
424 .color = YELLOW },
425 };
426
427 std::vector<std::vector<int>> zorders = {
428 #ifdef DISPLAY_COMMUNITY
429 { 3, 2, 1 }, { 1, 3, 2 }, { 3, 1, 2 }, { 1, 2, 3 }, { 2, 1, 3 }, { 9, 100, 3 },
430 #else
431 { 0, 2, 1 }, { 1, 0, 2 }, { 0, 1, 2 }, { 1, 2, 0 }, { 2, 1, 0 },
432 #endif // DISPLAY_COMMUNITY
433 };
434 std::vector<std::shared_ptr<HdiTestLayer>> layers = CreateLayers(settings);
435
436 for (const auto& zorderList : zorders) {
437 // adjust the zorder
438 for (uint32_t i = 0; i < zorderList.size(); i++) {
439 settings[i].zorder = zorderList[i];
440 layers[i]->SetZorder(zorderList[i]);
441 }
442 std::vector<LayerSettings> tempSettings = settings;
443 std::sort(tempSettings.begin(), tempSettings.end(),
444 [=](const auto& l, auto const& r) { return l.zorder < r.zorder; });
445 // present and check
446 PresentAndCheck(tempSettings);
447 }
448 HdiTestDevice::GetInstance().Clear();
449 }
450
451 #ifdef DISPLAY_COMMUNITY
TEST_P(LayerRotateTest,SplitCheck)452 TEST_P(LayerRotateTest, SplitCheck)
453 {
454 LayerSettings settings = GetParam();
455 std::vector<LayerSettings> layersSetting = { settings };
456 std::vector<uint32_t> splitColors = { RED, GREEN, YELLOW, TRANSPARENT };
457 std::vector<std::shared_ptr<HdiTestLayer>> layers = CreateLayers(layersSetting);
458 ASSERT_TRUE((layers.size() > 0));
459 // split the buffer
460 auto layer = layers[0];
461 HdiGrallocBuffer* handle = layer->GetBackBuffer(); // the backbuffer has not present now
462 ASSERT_TRUE((handle != nullptr));
463 auto splitRects = SplitBuffer(*(handle->Get()), splitColors);
464 ASSERT_TRUE(splitRects.size() == splitColors.size()); // ensure the splitRects size
465 PrepareAndPrensent();
466 // change the rect and color to clockwise.
467 std::swap(splitColors[2], splitColors[1]);
468 std::swap(splitColors[2], splitColors[3]);
469 std::swap(splitRects[2], splitRects[1]);
470 std::swap(splitRects[2], splitRects[3]);
471
472 // rotation is clockwise,the color will change clockwise, we just change the color start index
473 uint32_t startIndex = 0;
474 switch (settings.rotate) {
475 case ROTATE_90:
476 startIndex = 3; // start form color index 3
477 break;
478 case ROTATE_180:
479 startIndex = 2; // start form color index 2
480 break;
481 case ROTATE_270:
482 startIndex = 1; // start form color index 1
483 break;
484 default:
485 startIndex = 0;
486 break;
487 }
488 std::vector<LayerSettings> layersCheck;
489 for (uint32_t i = 0; i < splitRects.size(); i++) {
490 uint32_t color = splitColors[(i + startIndex) % 4];
491 layersCheck.push_back({.displayRect = splitRects[i], .color = color});
492 }
493 ASSERT_TRUE((handle != nullptr));
494 /* for rotation may scale the buffer, Near the edge of rect the color will Smooth gradient,
495 so we must use the center to check.
496 */
497 PresentAndCheck(layersCheck, HdiCompositionCheck::CHECK_CENTER);
498 }
499 #endif // DISPLAY_COMMUNITY
500
TEST_F(DeviceTest,crop)501 TEST_F(DeviceTest, crop)
502 {
503 std::vector<LayerSettings> settings = {
504 {
505 .rectRatio = { 0, 0, 1.0f, 1.0f },
506 .color = RED },
507 };
508 std::vector<uint32_t> splitColors = { { RED, GREEN, YELLOW, TRANSPARENT } };
509
510 std::vector<std::shared_ptr<HdiTestLayer>> layers = CreateLayers(settings);
511 ASSERT_TRUE((layers.size() > 0));
512 // split the buffer
513 auto layer = layers[0];
514 HdiGrallocBuffer* handle = layer->GetBackBuffer(); // the backbuffer has not present now
515 ASSERT_TRUE((handle != nullptr));
516 auto splitRects = SplitBuffer(*(handle->Get()), splitColors);
517 PrepareAndPrensent();
518 for (uint32_t i = 0; i < splitRects.size(); i++) {
519 settings[0].color = splitColors[i];
520 layer->SetLayerCrop(splitRects[i]);
521 PresentAndCheck(settings);
522 }
523 }
524
TEST_F(DeviceTest,CtrlTest)525 TEST_F(DeviceTest, CtrlTest)
526 {
527 int ret;
528 DISPLAY_TEST_LOGD();
529 std::shared_ptr<HdiTestDisplay> display = HdiTestDevice::GetInstance().GetFirstDisplay();
530 ASSERT_TRUE(display != nullptr) << "get display failed";
531 ret = display->RegDisplayVBlankCallback(TestVBlankCallback, nullptr);
532 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "RegDisplayVBlankCallback failed";
533 ret = display->SetDisplayVsyncEnabled(true);
534 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "SetDisplayVsyncEnabled failed";
535 ret = VblankCtr::GetInstance().WaitVblank(SLEEP_CONT_2000); // 2000ms
536 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "WaitVblank timeout";
537 ret = display->SetDisplayVsyncEnabled(false);
538 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "SetDisplayVsyncEnabled failed";
539 usleep(SLEEP_CONT_100 * SLEEP_CONT_2000); // wait for 100ms avoid the last vsync.
540 ret = VblankCtr::GetInstance().WaitVblank(SLEEP_CONT_2000); // 2000ms
541 ASSERT_TRUE(ret != DISPLAY_SUCCESS) << "vblank do not disable";
542 }
543
544 INSTANTIATE_TEST_SUITE_P(MultiLayer, DeviceLayerDisplay, ::testing::ValuesIn(TEST_MULTILAYER));
545 INSTANTIATE_TEST_SUITE_P(SingleLayer, DeviceLayerDisplay, ::testing::ValuesIn(TEST_SINGLE_LAYER));
546 INSTANTIATE_TEST_SUITE_P(ScaleLayer, DeviceLayerDisplay, ::testing::ValuesIn(TEST_SCALE));
547 INSTANTIATE_TEST_SUITE_P(LayerAlpha, DeviceLayerDisplay, ::testing::ValuesIn(TEST_ALPHA));
548 #ifdef DISPLAY_COMMUNITY
549 INSTANTIATE_TEST_SUITE_P(VideoLayer, DeviceLayerDisplay, ::testing::ValuesIn(TEST_VIDEO));
550 INSTANTIATE_TEST_SUITE_P(Rotation, LayerRotateTest, ::testing::ValuesIn(TEST_ROTATE));
551 #endif // DISPLAY_COMMUNITY
552