1 #include "gtest/gtest.h"
10 #include "tdm_config.h"
11 #include "tdm_helper.h"
12 #include "tdm_backend.h"
13 #include "tbm_bufmgr.h"
14 #include "tbm_surface.h"
15 #include "tbm_surface_internal.h"
16 #include "tbm_drm_helper.h"
19 #define TMP_PATH_FOR_UTEST "/tmp/tmp_utest_helper"
22 class TDMHelper : public ::testing::Test {
24 tdm_display *dpy = NULL;
25 tbm_bufmgr bufmgr = NULL;
27 /*list of connected outputs*/
29 const tdm_output_mode **preferred_mode_array = NULL;
32 tbm_surface_h surface;
35 setenv("XDG_RUNTIME_DIR", "/run", 1);
36 setenv("TBM_DISPLAY_SERVER", "1", 1);
37 tdm_config_set_int(TDM_CONFIG_KEY_DEBUG_DLOG, 1);
38 tdm_config_set_int(TDM_CONFIG_KEY_GENERAL_THREAD, 1);
39 tdm_config_set_int(TDM_CONFIG_KEY_GENERAL_COMMIT_PER_VBLANK, 1);
44 unsetenv("XDG_RUNTIME_DIR");
45 unsetenv("TBM_DISPLAY_SERVER");
50 const tdm_output_mode *preferred_mode = NULL;
51 tdm_error error = TDM_ERROR_NONE;
52 int all_output_count = 0;
56 bufmgr = tbm_bufmgr_init(-1);
57 ASSERT_FALSE(bufmgr == NULL);
59 dpy = tdm_display_init(&error);
60 ASSERT_TRUE(error == TDM_ERROR_NONE);
61 ASSERT_FALSE(dpy == NULL);
63 master_fd = tbm_drm_helper_get_master_fd();
64 ASSERT_TRUE(tdm_display_get_output_count(dpy, &all_output_count) == TDM_ERROR_NONE);
66 outputs = (tdm_output **)calloc(all_output_count, sizeof(tdm_output *));
67 ASSERT_FALSE(NULL == outputs);
69 preferred_mode_array = (const tdm_output_mode **)calloc(all_output_count, sizeof(tdm_output_mode *));
70 ASSERT_FALSE(NULL == preferred_mode_array);
74 for (int i = 0; i < all_output_count; i++) {
75 tdm_output *output = tdm_display_get_output(dpy, i, &error);
76 int output_modes_cnt = 0;
77 const tdm_output_mode *output_modes;
79 if (TDM_ERROR_NONE != error || NULL == output)
82 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
83 if (TDM_ERROR_NONE != tdm_output_get_conn_status(output, &status))
86 if (status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
89 error = tdm_output_get_available_modes(output, &output_modes, &output_modes_cnt);
90 if (TDM_ERROR_NONE != error)
92 if (output_modes_cnt <= 0) {
96 for(int j = 0; j < output_modes_cnt; j++)
97 if(output_modes[j].type & TDM_OUTPUT_MODE_TYPE_PREFERRED)
98 preferred_mode = &output_modes[j];
103 if (preferred_mode_array)
104 preferred_mode_array[output_count] = preferred_mode;
106 outputs[output_count] = output;
109 error = tdm_output_set_mode(output, preferred_mode);
110 ASSERT_EQ(TDM_ERROR_NONE, error);
112 surface = tbm_surface_create(256, 256, TBM_FORMAT_ARGB8888);
113 ASSERT_TRUE(surface != NULL);
119 while (tbm_surface_internal_is_valid(surface))
120 tbm_surface_destroy(surface);
122 tdm_display_deinit(dpy);
124 tbm_bufmgr_deinit(bufmgr);
128 if (preferred_mode_array)
129 free(preferred_mode_array);
130 if (master_fd > -1) {
131 int temp_master_fd = tbm_drm_helper_get_master_fd();
132 EXPECT_EQ(temp_master_fd, -1) << "Fatal Error. Can't deinit tdm/tbm" << std::endl;
133 if (temp_master_fd > -1)
142 int remove_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
144 int rv = remove(fpath);
150 int rmrf(const char *path)
152 return nftw(path, remove_cb, 64, FTW_DEPTH | FTW_PHYS);
155 class TDMHelperSurface: public TDMHelper
162 ASSERT_EQ(0, mkdir(TMP_PATH_FOR_UTEST, 0755)) << "error: " << strerror(errno);
167 rmrf(TMP_PATH_FOR_UTEST);
168 TDMHelper::TearDown();
172 class TDMHelperDisplay: public TDMHelper {
175 class TDMHelperOutput: public TDMHelper {
179 /* double tdm_helper_get_time(void); */
180 TEST(TDMHelper, GetTime)
183 time = tdm_helper_get_time();
187 /* void tdm_helper_dump_start(char *dumppath, int *count); */
188 TEST(TDMHelper, DumpStartFailNull)
191 tdm_helper_dump_start(NULL, &count);
193 tdm_helper_dump_start((char *)TMP_PATH_FOR_UTEST, NULL);
196 TEST(TDMHelper, DumpStartSuccessful)
199 tdm_helper_dump_start((char *)TMP_PATH_FOR_UTEST, &count);
202 /* void tdm_helper_dump_stop(void); */
203 TEST(TDMHelper, DumpStopSuccessful)
205 tdm_helper_dump_stop();
208 /* void tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file); */
209 TEST_F(TDMHelperSurface, DumpBufferFailNull)
211 tdm_helper_dump_buffer(NULL, TMP_PATH_FOR_UTEST "/tmp");
214 TEST_F(TDMHelperSurface, DumpBufferFailInvalidOutput)
217 tdm_helper_dump_buffer((tbm_surface_h)&invalid_surface, TMP_PATH_FOR_UTEST "/xyz.png");
219 /* file name mast have an extension ".png" */
220 tdm_helper_dump_buffer(surface, TMP_PATH_FOR_UTEST "/tmp.xyz");
223 TEST_F(TDMHelperSurface, DumpBufferFailUnknownFormat)
226 surf = tbm_surface_create(256, 256, TBM_FORMAT_YUV444);
227 ASSERT_TRUE(surf != NULL);
228 tdm_helper_dump_buffer(surface, TMP_PATH_FOR_UTEST "/xyz.png");
229 tbm_surface_destroy(surf);
232 TEST_F(TDMHelperSurface, DumpBufferSuccessful)
236 tdm_helper_dump_buffer(surface, TMP_PATH_FOR_UTEST "/rgb.png");
238 surf = tbm_surface_create(256, 256, TBM_FORMAT_YUV420);
239 ASSERT_TRUE(surf != NULL);
240 tdm_helper_dump_buffer(surf, TMP_PATH_FOR_UTEST "/YUV.yuv");
241 tbm_surface_destroy(surf);
243 surf = tbm_surface_create(256, 256, TBM_FORMAT_NV12);
244 ASSERT_TRUE(surf != NULL);
245 tdm_helper_dump_buffer(surf, TMP_PATH_FOR_UTEST "/NV.yuv");
246 tbm_surface_destroy(surf);
248 surf = tbm_surface_create(256, 256, TBM_FORMAT_YUYV);
249 ASSERT_TRUE(surf != NULL);
250 tdm_helper_dump_buffer(surf, TMP_PATH_FOR_UTEST "/YUYV.yuv");
251 tbm_surface_destroy(surf);
254 /* void tdm_helper_clear_buffer_color(tbm_surface_h buffer, tdm_pos *pos, unsigned int color); */
255 TEST_F(TDMHelperSurface, ClearBufferColorFailNull)
258 tdm_helper_clear_buffer_color(NULL, &pos, 0x0F0F0F);
261 TEST_F(TDMHelperSurface, ClearBufferColorInvalidOutput)
265 tdm_helper_clear_buffer_color((tbm_surface_h)&invalid_surface, &pos, 0x0F0F0F);
268 TEST_F(TDMHelperSurface, ClearBufferColorUnknownFormat)
270 tdm_pos pos = { .x = 0, .y = 0, .w = 10, .h = 10 };
272 surf = tbm_surface_create(256, 256, TBM_FORMAT_YUV444);
273 ASSERT_TRUE(surf != NULL);
274 tdm_helper_clear_buffer_color(surf, &pos, 0x0F0F0F);
275 tbm_surface_destroy(surf);
278 TEST_F(TDMHelperSurface, ClearBufferColorSuccessful)
280 tdm_pos pos = { .x = 0, .y = 0, .w = 10, .h = 10 };
283 tdm_helper_clear_buffer_color(surface, &pos, 0x0F0F0F);
285 surf = tbm_surface_create(256, 256, TBM_FORMAT_YVU420);
286 ASSERT_TRUE(surf != NULL);
287 tdm_helper_clear_buffer_color(surf, &pos, 0x0F0F0F);
288 tbm_surface_destroy(surf);
290 surf = tbm_surface_create(256, 256, TBM_FORMAT_NV12);
291 ASSERT_TRUE(surf != NULL);
292 tdm_helper_clear_buffer_color(surf, &pos, 0x0F0F0F);
293 tbm_surface_destroy(surf);
295 surf = tbm_surface_create(256, 256, TBM_FORMAT_YUYV);
296 ASSERT_TRUE(surf != NULL);
297 tdm_helper_clear_buffer_color(surf, &pos, 0x0F0F0F);
298 tbm_surface_destroy(surf);
300 surf = tbm_surface_create(256, 256, TBM_FORMAT_UYVY);
301 ASSERT_TRUE(surf != NULL);
302 tdm_helper_clear_buffer_color(surf, &pos, 0x0F0F0F);
303 tbm_surface_destroy(surf);
306 /* void tdm_helper_clear_buffer_pos(tbm_surface_h buffer, tdm_pos *pos); */
307 TEST_F(TDMHelperSurface, ClearBufferPosFailNull)
310 tdm_helper_clear_buffer_pos(NULL, &pos);
313 TEST_F(TDMHelperSurface, ClearBufferPosInvalidOutput)
317 tdm_helper_clear_buffer_pos((tbm_surface_h)&invalid_surface, &pos);
320 TEST_F(TDMHelperSurface, ClearBufferPosUnknownFormat)
322 tdm_pos pos = { .x = 0, .y = 0, .w = 10, .h = 10 };
324 surf = tbm_surface_create(256, 256, TBM_FORMAT_YUV444);
325 ASSERT_TRUE(surf != NULL);
326 tdm_helper_clear_buffer_pos(surf, &pos);
327 tbm_surface_destroy(surf);
330 TEST_F(TDMHelperSurface, ClearBufferPosSuccessful)
332 tdm_pos pos = { .x = 0, .y = 0, .w = 10, .h = 10 };
335 tdm_helper_clear_buffer_pos(surface, &pos);
337 surf = tbm_surface_create(256, 256, TBM_FORMAT_YVU420);
338 ASSERT_TRUE(surf != NULL);
339 tdm_helper_clear_buffer_pos(surf, &pos);
340 tbm_surface_destroy(surf);
342 surf = tbm_surface_create(256, 256, TBM_FORMAT_NV12);
343 ASSERT_TRUE(surf != NULL);
344 tdm_helper_clear_buffer_pos(surf, &pos);
345 tbm_surface_destroy(surf);
347 surf = tbm_surface_create(256, 256, TBM_FORMAT_YUYV);
348 ASSERT_TRUE(surf != NULL);
349 tdm_helper_clear_buffer_pos(surf, &pos);
350 tbm_surface_destroy(surf);
352 surf = tbm_surface_create(256, 256, TBM_FORMAT_UYVY);
353 ASSERT_TRUE(surf != NULL);
354 tdm_helper_clear_buffer_pos(surf, &pos);
355 tbm_surface_destroy(surf);
358 /* void tdm_helper_clear_buffer(tbm_surface_h buffer); */
359 TEST_F(TDMHelperSurface, ClearBufferFailNull)
361 tdm_helper_clear_buffer(NULL);
364 TEST_F(TDMHelperSurface, ClearBufferInvalidOutput)
367 tdm_helper_clear_buffer((tbm_surface_h)&invalid_surface);
370 TEST_F(TDMHelperSurface, ClearBufferUnknownFormat)
374 surf = tbm_surface_create(256, 256, TBM_FORMAT_YUV444);
375 ASSERT_TRUE(surf != NULL);
376 tdm_helper_clear_buffer(surf);
377 tbm_surface_destroy(surf);
380 TEST_F(TDMHelperSurface, ClearBufferSuccessful)
384 tdm_helper_clear_buffer(surface);
386 surf = tbm_surface_create(256, 256, TBM_FORMAT_YVU420);
387 ASSERT_TRUE(surf != NULL);
388 tdm_helper_clear_buffer(surf);
389 tbm_surface_destroy(surf);
391 surf = tbm_surface_create(256, 256, TBM_FORMAT_NV12);
392 ASSERT_TRUE(surf != NULL);
393 tdm_helper_clear_buffer(surf);
394 tbm_surface_destroy(surf);
396 surf = tbm_surface_create(256, 256, TBM_FORMAT_YUYV);
397 ASSERT_TRUE(surf != NULL);
398 tdm_helper_clear_buffer(surf);
399 tbm_surface_destroy(surf);
401 surf = tbm_surface_create(256, 256, TBM_FORMAT_UYVY);
402 ASSERT_TRUE(surf != NULL);
403 tdm_helper_clear_buffer(surf);
404 tbm_surface_destroy(surf);
407 /* void tdm_helper_get_buffer_full_size(tbm_surface_h buffer, int *buffer_w, int *buffer_h); */
408 TEST_F(TDMHelperSurface, GetBufferFullSizeFailNull)
412 tdm_helper_get_buffer_full_size(NULL, &buffer_w, &buffer_h);
415 TEST_F(TDMHelperSurface, GetBufferFullSizeFailinvalidBuffer)
420 tdm_helper_get_buffer_full_size((tbm_surface_h)&invalid_surface, &buffer_w, &buffer_h);
423 TEST_F(TDMHelperSurface, GetBufferFullSizeSuccessful)
427 tdm_helper_get_buffer_full_size(surface, &buffer_w, &buffer_h);
428 ASSERT_GE(buffer_w, 0);
429 ASSERT_GE(buffer_h, 0);
432 /* tdm_error tdm_helper_convert_buffer(tbm_surface_h srcbuf, tbm_surface_h dstbuf,
433 tdm_pos *srcpos, tdm_pos *dstpos,
434 tdm_transform transform, int over); */
435 TEST_F(TDMHelperSurface, ConvertBufferFailNull)
437 tdm_pos srcpos = { 0, 0, 256, 256 };
438 tdm_pos dstpos = { 0, 0, 256, 256 };
439 tbm_surface_h dstbuf;
442 dstbuf = tbm_surface_create(256, 256, TBM_FORMAT_ARGB8888);
443 ASSERT_TRUE(dstbuf != NULL);
445 error = tdm_helper_convert_buffer(NULL, dstbuf, &srcpos, &dstpos, TDM_TRANSFORM_NORMAL, 0);
446 tbm_surface_destroy(dstbuf);
447 ASSERT_NE(TDM_ERROR_NONE, error);
449 error = tdm_helper_convert_buffer(surface, NULL, &srcpos, &dstpos, TDM_TRANSFORM_NORMAL, 0);
450 ASSERT_NE(TDM_ERROR_NONE, error);
453 TEST_F(TDMHelperSurface, ConvertBufferFailinvalidBuffer)
456 tdm_pos srcpos = { 0, 0, 256, 256 };
457 tdm_pos dstpos = { 0, 0, 256, 256 };
458 tbm_surface_h dstbuf;
461 dstbuf = tbm_surface_create(256, 256, TBM_FORMAT_ARGB8888);
462 ASSERT_TRUE(dstbuf != NULL);
464 error = tdm_helper_convert_buffer((tbm_surface_h)&invalid_surface, dstbuf, &srcpos, &dstpos, TDM_TRANSFORM_NORMAL, 0);
465 tbm_surface_destroy(dstbuf);
466 ASSERT_NE(TDM_ERROR_NONE, error);
468 error = tdm_helper_convert_buffer(surface, (tbm_surface_h)&invalid_surface, &srcpos, &dstpos, TDM_TRANSFORM_NORMAL, 0);
469 ASSERT_NE(TDM_ERROR_NONE, error);
472 TEST_F(TDMHelperSurface, ConvertBufferSuccessful)
474 tdm_pos srcpos = { 0, 0, 256, 256 };
475 tdm_pos dstpos = { 0, 0, 256, 256 };
476 tbm_surface_h dstbuf;
479 dstbuf = tbm_surface_create(256, 256, TBM_FORMAT_ARGB8888);
480 ASSERT_TRUE(dstbuf != NULL);
482 error = tdm_helper_convert_buffer(surface, dstbuf, &srcpos, &dstpos, TDM_TRANSFORM_NORMAL, 0);
483 tbm_surface_destroy(dstbuf);
484 ASSERT_EQ(TDM_ERROR_NONE, error);
488 /* void tdm_helper_get_display_information(tdm_display *dpy, char *reply, int *len); */
489 TEST_F(TDMHelperDisplay, GetDisplayInformationFailNull)
491 char reply[STR_LEN] = {0};
493 tdm_helper_get_display_information(NULL, reply, &len);
494 ASSERT_LT(len, STR_LEN);
495 ASSERT_EQ((STR_LEN - len), strlen(reply));
498 tdm_helper_get_display_information(dpy, NULL, &len);
499 ASSERT_EQ(STR_LEN, len);
502 tdm_helper_get_display_information(dpy, reply, NULL);
503 ASSERT_EQ(0, reply[0]);
506 TEST_F(TDMHelperDisplay, GetDisplayInformationSuccessful)
508 char reply[STR_LEN] = {0};
510 tdm_helper_get_display_information(dpy, reply, &len);
511 ASSERT_LT(len, STR_LEN);
512 ASSERT_EQ((STR_LEN - len), strlen(reply));
514 int capture_handler_is_called = 0;
515 void capture_handler(tbm_surface_h buffer, void *user_data)
517 if (&capture_handler_is_called == user_data)
518 capture_handler_is_called = 1;
520 /* tdm_error tdm_helper_capture_output(...); */
521 TEST_F(TDMHelperOutput, CaptureOutputFailNull)
523 int output_count = 0;
524 tdm_output *output = NULL;
526 tbm_surface_h buffer = NULL;
528 error = tdm_display_get_output_count(dpy, &output_count);
529 ASSERT_EQ(TDM_ERROR_NONE, error);
530 for (int i = 0; i < output_count; i++) {
532 output = tdm_display_get_output(dpy, i, &error);
533 ASSERT_NE(NULL, output);
535 error = tdm_helper_capture_output(NULL, buffer, 0, 0, 0, 0, capture_handler, &capture_handler_is_called);
536 ASSERT_NE(TDM_ERROR_NONE, error);
537 error = tdm_helper_capture_output(output, NULL, 0, 0, 0, 0, capture_handler, &capture_handler_is_called);
538 ASSERT_NE(TDM_ERROR_NONE, error);
539 error = tdm_helper_capture_output(output, buffer, 0, 0, 0, 0, NULL, &capture_handler_is_called);
540 ASSERT_NE(TDM_ERROR_NONE, error);
541 error = tdm_helper_capture_output(output, buffer, 0, 0, 0, 0, capture_handler, NULL);
542 ASSERT_NE(TDM_ERROR_NONE, error);
546 TEST_F(TDMHelperOutput, CaptureOutputFailInvalidInputs)
548 for (int i = 0; i < output_count; i++) {
549 error = tdm_helper_capture_output(outputs[i], surface, -1, 0, 0, 0, capture_handler, &capture_handler_is_called);
550 ASSERT_NE(TDM_ERROR_NONE, error);
551 error = tdm_helper_capture_output(outputs[i], surface, 0, -1, 0, 0, capture_handler, &capture_handler_is_called);
552 ASSERT_NE(TDM_ERROR_NONE, error);
553 error = tdm_helper_capture_output(outputs[i], surface, 0, 0, -1, 0, capture_handler, &capture_handler_is_called);
554 ASSERT_NE(TDM_ERROR_NONE, error);
555 error = tdm_helper_capture_output(outputs[i], surface, 0, 0, 0, -1, capture_handler, &capture_handler_is_called);
556 ASSERT_NE(TDM_ERROR_NONE, error);
560 TEST_F(TDMHelperOutput, CaptureOutputSuccessful)
562 for (int i = 0; i < output_count; i++) {
563 error = tdm_helper_capture_output(outputs[i], surface, 0, 0, 255, 255, capture_handler, &capture_handler_is_called);
564 ASSERT_EQ(TDM_ERROR_NONE, error);
568 /* int tdm_helper_output_commit_per_vblank_enabled(tdm_output *output); */
569 TEST_F(TDMHelperOutput, CommitPerVblankEnabledFailNull)
572 state = tdm_helper_output_commit_per_vblank_enabled(NULL);
573 ASSERT_EQ(-1, state);
576 TEST_F(TDMHelperOutput, CommitPerVblankEnabledSuccessful)
580 for (int i = 0; i < output_count; i++) {
581 state = tdm_helper_output_commit_per_vblank_enabled(outputs[i]);