2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <dali-toolkit-test-suite-utils.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/public-api/controls/gl-view/gl-view.h>
25 #include <dali/devel-api/adaptor-framework/window-devel.h>
26 #include <dali/public-api/signals/render-callback.h>
29 using namespace Dali::Toolkit;
31 // Positive test case for a method
32 int UtcDaliGlViewDirectRenderingNew(void)
34 ToolkitTestApplication application;
35 tet_infoline(" UtcDaliGlViewDirectRenderingNew");
36 GlView view = GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGBA8888);
37 DALI_TEST_CHECK(view);
39 auto mode1 = view.GetBackendMode();
41 DALI_TEST_EQUALS(mode1, GlView::BackendMode::DIRECT_RENDERING, TEST_LOCATION);
43 GlView view2 = GlView::New(GlView::BackendMode::EGL_IMAGE_OFFSCREEN_RENDERING, GlView::ColorFormat::RGBA8888);
44 DALI_TEST_CHECK(view2);
46 auto mode2 = view2.GetBackendMode();
47 DALI_TEST_EQUALS(mode2, GlView::BackendMode::EGL_IMAGE_OFFSCREEN_RENDERING, TEST_LOCATION);
52 int UtcDaliGlViewDirectRenderingNewN(void)
54 ToolkitTestApplication application;
55 tet_infoline(" UtcDaliGlViewDirectRenderingNewN");
56 // Invalid backend mode
57 GlView view = GlView::New(GlView::BackendMode(11111), GlView::ColorFormat::RGBA8888);
58 DALI_TEST_CHECK(!view);
63 // Positive test case for a method
64 int UtcDaliGlViewDirectRenderingDownCast(void)
66 ToolkitTestApplication application;
67 tet_infoline(" UtcDaliGlViewDirectRenderingDownCast");
69 GlView view = GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
70 BaseHandle handle(view);
72 Toolkit::GlView view2 = Toolkit::GlView::DownCast(handle);
73 DALI_TEST_CHECK(view);
74 DALI_TEST_CHECK(view2);
75 DALI_TEST_CHECK(view == view2);
79 int UtcDaliGlViewDirectRenderingCopyAndAssignment(void)
81 ToolkitTestApplication application;
82 tet_infoline("UtcDaliGlViewDirectRenderingCopyAndAssignment");
84 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
85 DALI_TEST_CHECK(view);
88 DALI_TEST_CHECK(view == copy);
91 DALI_TEST_CHECK(!assign);
94 DALI_TEST_CHECK(assign == view);
99 int UtcDaliGlViewDirectRenderingMoveAssignment(void)
101 ToolkitTestApplication application;
102 tet_infoline("UtcDaliGlViewDirectRenderingMoveAssignment");
104 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
105 DALI_TEST_EQUALS(1, view.GetBaseObject().ReferenceCount(), TEST_LOCATION);
108 moved = std::move(view);
109 DALI_TEST_CHECK(moved);
110 DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
111 DALI_TEST_CHECK(!view);
116 int UtcDaliGlViewDirectRenderingSetGraphicsConfigGles20N(void)
118 ToolkitTestApplication application;
119 tet_infoline("UtcDaliGlViewDirectRenderingSetGraphicsConfigGles20");
123 view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
124 DALI_TEST_CHECK(false);
128 DALI_TEST_CHECK(true);
133 int UtcDaliGlViewDirectRenderingSetGraphicsConfigGles30(void)
135 ToolkitTestApplication application;
136 tet_infoline("UtcDaliGlViewDirectRenderingSetGraphicsConfigGles30");
137 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
141 view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_3_0);
142 DALI_TEST_CHECK(true);
146 DALI_TEST_CHECK(false);
151 int UtcDaliGlViewDirectRenderingRenderingMode(void)
153 ToolkitTestApplication application;
154 tet_infoline("UtcDaliGlViewDirectRenderingRenderingMode");
155 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
157 view.SetRenderingMode(GlView::RenderingMode::ON_DEMAND);
159 GlView::RenderingMode mode = view.GetRenderingMode();
161 DALI_TEST_EQUALS(GlView::RenderingMode::ON_DEMAND, mode, TEST_LOCATION);
166 int UtcDaliGlViewDirectRenderingOnSizeSet(void)
168 ToolkitTestApplication application;
169 tet_infoline("UtcDaliGlViewDirectRenderingOnSizeSet");
170 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
172 application.GetScene().Add(view);
174 application.SendNotification();
175 application.Render();
177 Vector3 size(200.0f, 300.0f, 0.0f);
178 view.SetProperty(Actor::Property::SIZE, size);
180 application.SendNotification();
181 application.Render();
183 DALI_TEST_EQUALS(view.GetCurrentProperty<Vector3>(Actor::Property::SIZE), size, TEST_LOCATION);
188 namespace DirectRenderingCode
190 // Internal callback function
191 void glInit(Dali::RenderCallbackInput& input)
195 int glRenderFrame(Dali::RenderCallbackInput& input)
197 static unsigned int retFlag = 0;
201 int gBoundTextureCount = 0;
203 int glRenderFrameWithTextures(Dali::RenderCallbackInput& input)
205 gBoundTextureCount = input.textureBindings.size();
209 void glTerminate(Dali::RenderCallbackInput& input)
213 // Internal callback function
214 void glInitMT(Dali::RenderCallbackInput& input)
218 int gDRFramesRendered = 0;
220 int glRenderFrameMT(Dali::RenderCallbackInput& input)
226 void glTerminateMT(Dali::RenderCallbackInput& input)
230 void resizeCB(Vector2 size)
234 } // namespace DirectRenderingCode
236 int UtcDaliGlViewDirectRenderingRegisterGlCallbacksN(void)
238 ToolkitTestApplication application;
239 tet_infoline("UtcDaliGlViewDirectRenderingRegisterGlCallbacksN");
244 view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrame), Dali::MakeCallback(DirectRenderingCode::glTerminate));
245 DALI_TEST_CHECK(false);
249 DALI_TEST_CHECK(true);
254 int UtcDaliGlViewDirectRenderingSetResizeCallbackN(void)
256 ToolkitTestApplication application;
257 tet_infoline("UtcDaliGlViewDirectRenderingSetResizeCallback");
262 view.SetResizeCallback(Dali::MakeCallback(DirectRenderingCode::resizeCB));
263 DALI_TEST_CHECK(false);
267 DALI_TEST_CHECK(true);
272 int UtcDaliGlViewDirectRenderingRenderOnce(void)
274 ToolkitTestApplication application;
275 tet_infoline("UtcDaliGlViewDirectRenderingRenderOnce");
276 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
281 DALI_TEST_CHECK(true);
285 DALI_TEST_CHECK(false);
290 int UtcDaliGlViewDirectRenderingWindowVisibilityChanged(void)
292 ToolkitTestApplication application;
293 tet_infoline("UtcDaliGlViewDirectRenderingWindowVisibilityChanged");
294 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
295 application.GetScene().Add(view);
296 view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
297 view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
298 view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrame), Dali::MakeCallback(DirectRenderingCode::glTerminate));
299 view.SetResizeCallback(Dali::MakeCallback(DirectRenderingCode::resizeCB));
301 application.SendNotification();
302 application.Render();
304 Window window = DevelWindow::Get(view);
306 application.SendNotification();
307 application.Render();
309 DALI_TEST_CHECK(true);
313 int UtcDaliGlViewDirectRenderingOnScene(void)
315 ToolkitTestApplication application;
317 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
320 application.GetScene().Add(view);
321 view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
322 view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
323 view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrame), Dali::MakeCallback(DirectRenderingCode::glTerminate));
325 application.SendNotification();
326 application.Render();
329 application.GetScene().Remove(view);
331 application.SendNotification();
332 application.Render();
334 DALI_TEST_CHECK(true);
338 int UtcDaliGlViewDirectRenderingControlVisibilityChanged(void)
340 ToolkitTestApplication application;
342 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
343 application.GetScene().Add(view);
345 application.SendNotification();
346 application.Render();
348 view.SetProperty(Actor::Property::VISIBLE, false);
349 application.SendNotification();
350 application.Render();
351 DALI_TEST_CHECK(view.GetCurrentProperty<bool>(Actor::Property::VISIBLE) == false);
353 view.SetProperty(Actor::Property::VISIBLE, true);
354 application.SendNotification();
355 application.Render();
356 DALI_TEST_CHECK(view.GetCurrentProperty<bool>(Actor::Property::VISIBLE) == true);
361 int UtcDaliGlViewDirectRenderingResize(void)
363 ToolkitTestApplication application;
364 tet_infoline("UtcDaliGlViewDirectRenderingResize");
365 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
367 application.GetScene().Add(view);
368 view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
369 view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrame), Dali::MakeCallback(DirectRenderingCode::glTerminate));
370 view.SetResizeCallback(Dali::MakeCallback(DirectRenderingCode::resizeCB));
371 view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
372 view.SetProperty(Actor::Property::SIZE, Vector2(360.0f, 360.0f));
374 application.SendNotification();
375 application.Render();
377 //To GlViewRenderThread can recognize Resize signal the main thread have to sleep.
378 std::this_thread::sleep_for(std::chrono::milliseconds(100));
380 application.SendNotification();
381 application.Render();
383 DALI_TEST_CHECK(true);
387 int UtcDaliGlViewDirectRenderingDirectResize(void)
389 ToolkitTestApplication application;
390 tet_infoline("UtcDaliGlViewDirectRenderingResize");
391 GlView view = Toolkit::GlView::New(GlView::BackendMode::UNSAFE_DIRECT_RENDERING, GlView::ColorFormat::RGB888);
393 application.GetScene().Add(view);
394 view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
395 view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrame), Dali::MakeCallback(DirectRenderingCode::glTerminate));
396 view.SetResizeCallback(Dali::MakeCallback(DirectRenderingCode::resizeCB));
397 view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
398 view.SetProperty(Actor::Property::SIZE, Vector2(360.0f, 360.0f));
400 application.SendNotification();
401 application.Render();
403 //To GlViewRenderThread can recognize Resize signal the main thread have to sleep.
404 std::this_thread::sleep_for(std::chrono::milliseconds(100));
406 application.SendNotification();
407 application.Render();
409 DALI_TEST_CHECK(true);
413 int UtcDaliGlViewDirectRenderingTerminateCallback(void)
415 ToolkitTestApplication application;
416 tet_infoline("UtcDaliGlViewDirectRenderingTerminateCallback");
417 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
419 application.GetScene().Add(view);
420 view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
421 view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrame), Dali::MakeCallback(DirectRenderingCode::glTerminate));
422 view.SetResizeCallback(Dali::MakeCallback(DirectRenderingCode::resizeCB));
423 view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
424 view.SetProperty(Actor::Property::SIZE, Vector2(360.0f, 360.0f));
426 application.SendNotification();
427 application.Render();
429 //To GlViewRenderThread can recognize Resize signal the main thread have to sleep.
430 std::this_thread::sleep_for(std::chrono::milliseconds(100));
432 application.SendNotification();
433 application.Render();
435 DALI_TEST_CHECK(true);
439 int UtcDaliGlViewDirectRenderingTextureBinding(void)
441 ToolkitTestApplication application;
443 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
445 view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
446 view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
447 view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrameWithTextures), Dali::MakeCallback(DirectRenderingCode::glTerminate));
448 view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
449 view.SetProperty(Actor::Property::SIZE, Vector2(360.0f, 360.0f));
451 // Set size on the actor (half the window size to show that glClear() and scissor test work together)
452 view.SetProperty(Actor::Property::SIZE, Size(100, 100));
453 view.SetProperty(Actor::Property::POSITION, Vector2(0, 0));
455 application.GetScene().Add(view);
458 Texture texture1 = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::Format::RGBA8888, 512, 512);
459 auto* data1 = reinterpret_cast<uint8_t*>(malloc(512 * 512 * 4));
460 PixelData pixelData1 = PixelData::New(data1, 512 * 512 * 4, 512, 512, Pixel::Format::RGBA8888, PixelData::ReleaseFunction::FREE);
461 texture1.Upload(pixelData1);
464 Texture texture2 = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::Format::RGBA8888, 512, 512);
465 auto* data2 = reinterpret_cast<uint8_t*>(malloc(512 * 512 * 4));
466 PixelData pixelData2 = PixelData::New(data2, 512 * 512 * 4, 512, 512, Pixel::Format::RGBA8888, PixelData::ReleaseFunction::FREE);
467 texture2.Upload(pixelData2);
469 std::vector<Texture> texturesToBind;
470 texturesToBind.push_back(texture1);
471 texturesToBind.push_back(texture2);
473 view.BindTextureResources(texturesToBind);
475 DirectRenderingCode::gBoundTextureCount = 0;
477 application.SendNotification();
478 application.Render();
480 DALI_TEST_EQUALS(DirectRenderingCode::gBoundTextureCount, texturesToBind.size(), TEST_LOCATION);
485 // Positive test case for a method
486 int UtcDaliGlViewDirectRenderingThreadedNew(void)
488 ToolkitTestApplication application;
489 tet_infoline(" UtcDaliGlViewDirectRenderingThreadedNew");
490 GlView view = GlView::New(GlView::BackendMode::DIRECT_RENDERING_THREADED, GlView::ColorFormat::RGBA8888);
491 DALI_TEST_CHECK(view);
493 auto mode1 = view.GetBackendMode();
495 DALI_TEST_EQUALS(mode1, GlView::BackendMode::DIRECT_RENDERING_THREADED, TEST_LOCATION);
500 int UtcDaliGlViewDirectRenderingThreadedOnScene(void)
502 ToolkitTestApplication application;
504 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING_THREADED, GlView::ColorFormat::RGB888);
507 application.GetScene().Add(view);
508 view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
509 view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_3_0);
510 view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInitMT), Dali::MakeCallback(DirectRenderingCode::glRenderFrameMT), Dali::MakeCallback(DirectRenderingCode::glTerminateMT));
511 view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
512 view.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
514 // Set size on the actor (half the window size to show that glClear() and scissor test work together)
515 view.SetProperty(Actor::Property::SIZE, Size(100, 100));
516 view.SetProperty(Actor::Property::POSITION, Vector2(0, 0));
518 while(DirectRenderingCode::gDRFramesRendered < 1)
520 application.SendNotification();
521 application.Render();
523 DALI_TEST_CHECK(true);
527 extern "C" bool gDirectRenderingFailCreateShader;
528 extern "C" bool gDirectRenderingFailCreateProgram;
530 int UtcDaliGlViewDirectRenderingThreadedOnScene1(void)
532 ToolkitTestApplication application;
534 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING_THREADED, GlView::ColorFormat::RGB888);
536 // This test will fail instantiating shaders
537 gDirectRenderingFailCreateShader = true;
540 application.GetScene().Add(view);
541 view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
542 view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_3_0);
543 view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInitMT), Dali::MakeCallback(DirectRenderingCode::glRenderFrameMT), Dali::MakeCallback(DirectRenderingCode::glTerminateMT));
544 view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
545 view.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
547 // Set size on the actor (half the window size to show that glClear() and scissor test work together)
548 view.SetProperty(Actor::Property::SIZE, Size(100, 100));
549 view.SetProperty(Actor::Property::POSITION, Vector2(0, 0));
551 while(DirectRenderingCode::gDRFramesRendered < 1)
553 application.SendNotification();
554 application.Render();
556 DALI_TEST_CHECK(true);
560 int UtcDaliGlViewDirectRenderingThreadedOnScene2(void)
562 ToolkitTestApplication application;
564 GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING_THREADED, GlView::ColorFormat::RGB888);
566 // This test will fail instantiating shaders
567 gDirectRenderingFailCreateProgram = true;
570 application.GetScene().Add(view);
571 view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
572 view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_3_0);
573 view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInitMT), Dali::MakeCallback(DirectRenderingCode::glRenderFrameMT), Dali::MakeCallback(DirectRenderingCode::glTerminateMT));
574 view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
575 view.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
577 // Set size on the actor (half the window size to show that glClear() and scissor test work together)
578 view.SetProperty(Actor::Property::SIZE, Size(100, 100));
579 view.SetProperty(Actor::Property::POSITION, Vector2(0, 0));
581 while(DirectRenderingCode::gDRFramesRendered < 1)
583 application.SendNotification();
584 application.Render();
586 DALI_TEST_CHECK(true);