Merge "Fix npatch visual auxiliary bug" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-GlViewDirectRendering.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <unistd.h>
19 #include <thread>
20
21 #include <dali-toolkit-test-suite-utils.h>
22
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>
27
28
29 using namespace Dali;
30 using namespace Dali::Toolkit;
31
32
33 // Positive test case for a method
34 int UtcDaliGlViewDirectRenderingNew(void)
35 {
36   ToolkitTestApplication application;
37   tet_infoline(" UtcDaliGlViewDirectRenderingNew");
38   GlView view = GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGBA8888);
39   DALI_TEST_CHECK(view);
40
41   auto mode1 = view.GetBackendMode();
42
43   DALI_TEST_EQUALS(mode1, GlView::BackendMode::DIRECT_RENDERING, TEST_LOCATION);
44
45   GlView view2 = GlView::New(GlView::BackendMode::EGL_IMAGE_OFFSCREEN_RENDERING, GlView::ColorFormat::RGBA8888);
46   DALI_TEST_CHECK(view2);
47
48   auto mode2 = view2.GetBackendMode();
49   DALI_TEST_EQUALS(mode2, GlView::BackendMode::EGL_IMAGE_OFFSCREEN_RENDERING, TEST_LOCATION);
50
51   END_TEST;
52 }
53
54 int UtcDaliGlViewDirectRenderingNewN(void)
55 {
56   ToolkitTestApplication application;
57   tet_infoline(" UtcDaliGlViewDirectRenderingNewN");
58   // Invalid backend mode
59   GlView view = GlView::New(GlView::BackendMode(11111), GlView::ColorFormat::RGBA8888);
60   DALI_TEST_CHECK(!view);
61
62   END_TEST;
63 }
64
65 // Positive test case for a method
66 int UtcDaliGlViewDirectRenderingDownCast(void)
67 {
68   ToolkitTestApplication application;
69   tet_infoline(" UtcDaliGlViewDirectRenderingDownCast");
70
71   GlView     view = GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
72   BaseHandle handle(view);
73
74   Toolkit::GlView view2 = Toolkit::GlView::DownCast(handle);
75   DALI_TEST_CHECK(view);
76   DALI_TEST_CHECK(view2);
77   DALI_TEST_CHECK(view == view2);
78   END_TEST;
79 }
80
81 int UtcDaliGlViewDirectRenderingCopyAndAssignment(void)
82 {
83   ToolkitTestApplication application;
84   tet_infoline("UtcDaliGlViewDirectRenderingCopyAndAssignment");
85
86   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
87   DALI_TEST_CHECK(view);
88
89   GlView copy(view);
90   DALI_TEST_CHECK(view == copy);
91
92   GlView assign;
93   DALI_TEST_CHECK(!assign);
94
95   assign = copy;
96   DALI_TEST_CHECK(assign == view);
97
98   END_TEST;
99 }
100
101 int UtcDaliGlViewDirectRenderingMoveAssignment(void)
102 {
103   ToolkitTestApplication application;
104   tet_infoline("UtcDaliGlViewDirectRenderingMoveAssignment");
105
106   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
107   DALI_TEST_EQUALS(1, view.GetBaseObject().ReferenceCount(), TEST_LOCATION);
108
109   GlView moved;
110   moved = std::move(view);
111   DALI_TEST_CHECK(moved);
112   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
113   DALI_TEST_CHECK(!view);
114
115   END_TEST;
116 }
117
118 int UtcDaliGlViewDirectRenderingSetGraphicsConfigGles20N(void)
119 {
120   ToolkitTestApplication application;
121   tet_infoline("UtcDaliGlViewDirectRenderingSetGraphicsConfigGles20");
122   GlView view;
123   try
124   {
125     view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
126     DALI_TEST_CHECK(false);
127   }
128   catch(...)
129   {
130     DALI_TEST_CHECK(true);
131   }
132   END_TEST;
133 }
134
135 int UtcDaliGlViewDirectRenderingSetGraphicsConfigGles30(void)
136 {
137   ToolkitTestApplication application;
138   tet_infoline("UtcDaliGlViewDirectRenderingSetGraphicsConfigGles30");
139   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
140
141   try
142   {
143     view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_3_0);
144     DALI_TEST_CHECK(true);
145   }
146   catch(...)
147   {
148     DALI_TEST_CHECK(false);
149   }
150   END_TEST;
151 }
152
153 int UtcDaliGlViewDirectRenderingRenderingMode(void)
154 {
155   ToolkitTestApplication application;
156   tet_infoline("UtcDaliGlViewDirectRenderingRenderingMode");
157   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
158
159   view.SetRenderingMode(GlView::RenderingMode::ON_DEMAND);
160
161   GlView::RenderingMode mode = view.GetRenderingMode();
162
163   DALI_TEST_EQUALS(GlView::RenderingMode::ON_DEMAND, mode, TEST_LOCATION);
164
165   END_TEST;
166 }
167
168 int UtcDaliGlViewDirectRenderingOnSizeSet(void)
169 {
170   ToolkitTestApplication application;
171   tet_infoline("UtcDaliGlViewDirectRenderingOnSizeSet");
172   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
173
174   application.GetScene().Add(view);
175
176   application.SendNotification();
177   application.Render();
178
179   Vector3 size(200.0f, 300.0f, 0.0f);
180   view.SetProperty(Actor::Property::SIZE, size);
181
182   application.SendNotification();
183   application.Render();
184
185   DALI_TEST_EQUALS(view.GetCurrentProperty<Vector3>(Actor::Property::SIZE), size, TEST_LOCATION);
186
187   END_TEST;
188 }
189
190 namespace DirectRenderingCode
191 {
192
193 // Internal callback function
194 void glInit(void)
195 {
196 }
197
198 int glRenderFrame(void)
199 {
200   static unsigned int retFlag = 0;
201   return retFlag++;
202 }
203
204 void glTerminate(void)
205 {
206 }
207
208
209 // Internal callback function
210 void glInitMT(Dali::RenderCallbackInput& input)
211 {
212 }
213
214 int gDRFramesRendered = 0;
215
216 int glRenderFrameMT(Dali::RenderCallbackInput& input)
217 {
218   gDRFramesRendered++;
219   return 1;
220 }
221
222 void glTerminateMT(Dali::RenderCallbackInput& input)
223 {
224 }
225
226 void resizeCB(Vector2 size)
227 {
228 }
229
230 }
231
232 int UtcDaliGlViewDirectRenderingRegisterGlCallbacksN(void)
233 {
234   ToolkitTestApplication application;
235   tet_infoline("UtcDaliGlViewDirectRenderingRegisterGlCallbacksN");
236   GlView view;
237
238   try
239   {
240     view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrame), Dali::MakeCallback(DirectRenderingCode::glTerminate));
241     DALI_TEST_CHECK(false);
242   }
243   catch(...)
244   {
245     DALI_TEST_CHECK(true);
246   }
247   END_TEST;
248 }
249
250 int UtcDaliGlViewDirectRenderingSetResizeCallbackN(void)
251 {
252   ToolkitTestApplication application;
253   tet_infoline("UtcDaliGlViewDirectRenderingSetResizeCallback");
254   GlView view;
255
256   try
257   {
258     view.SetResizeCallback(Dali::MakeCallback(DirectRenderingCode::resizeCB));
259     DALI_TEST_CHECK(false);
260   }
261   catch(...)
262   {
263     DALI_TEST_CHECK(true);
264   }
265   END_TEST;
266 }
267
268 int UtcDaliGlViewDirectRenderingRenderOnce(void)
269 {
270   ToolkitTestApplication application;
271   tet_infoline("UtcDaliGlViewDirectRenderingRenderOnce");
272   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
273
274   try
275   {
276     view.RenderOnce();
277     DALI_TEST_CHECK(true);
278   }
279   catch(...)
280   {
281     DALI_TEST_CHECK(false);
282   }
283   END_TEST;
284 }
285
286 int UtcDaliGlViewDirectRenderingWindowVisibilityChanged(void)
287 {
288   ToolkitTestApplication application;
289   tet_infoline("UtcDaliGlViewDirectRenderingWindowVisibilityChanged");
290   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
291   application.GetScene().Add(view);
292   view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
293   view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
294   view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrame), Dali::MakeCallback(DirectRenderingCode::glTerminate));
295   view.SetResizeCallback(Dali::MakeCallback(DirectRenderingCode::resizeCB));
296
297   application.SendNotification();
298   application.Render();
299
300   Window window = DevelWindow::Get(view);
301   window.Hide();
302   application.SendNotification();
303   application.Render();
304
305   DALI_TEST_CHECK(true);
306   END_TEST;
307 }
308
309 int UtcDaliGlViewDirectRenderingOnScene(void)
310 {
311   ToolkitTestApplication application;
312
313   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
314
315   //Onscene
316   application.GetScene().Add(view);
317   view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
318   view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
319   view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrame), Dali::MakeCallback(DirectRenderingCode::glTerminate));
320
321   application.SendNotification();
322   application.Render();
323
324   //Offscene
325   application.GetScene().Remove(view);
326
327   application.SendNotification();
328   application.Render();
329
330   DALI_TEST_CHECK(true);
331   END_TEST;
332 }
333
334 int UtcDaliGlViewDirectRenderingControlVisibilityChanged(void)
335 {
336   ToolkitTestApplication application;
337
338   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
339   application.GetScene().Add(view);
340
341   application.SendNotification();
342   application.Render();
343
344   view.SetProperty(Actor::Property::VISIBLE, false);
345   application.SendNotification();
346   application.Render();
347   DALI_TEST_CHECK(view.GetCurrentProperty<bool>(Actor::Property::VISIBLE) == false);
348
349   view.SetProperty(Actor::Property::VISIBLE, true);
350   application.SendNotification();
351   application.Render();
352   DALI_TEST_CHECK(view.GetCurrentProperty<bool>(Actor::Property::VISIBLE) == true);
353
354   END_TEST;
355 }
356
357 int UtcDaliGlViewDirectRenderingResize(void)
358 {
359   ToolkitTestApplication application;
360   tet_infoline("UtcDaliGlViewDirectRenderingResize");
361   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
362
363   application.GetScene().Add(view);
364   view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
365   view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrame), Dali::MakeCallback(DirectRenderingCode::glTerminate));
366   view.SetResizeCallback(Dali::MakeCallback(DirectRenderingCode::resizeCB));
367   view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
368   view.SetProperty(Actor::Property::SIZE, Vector2(360.0f, 360.0f));
369
370   application.SendNotification();
371   application.Render();
372
373   //To GlViewRenderThread can recognize Resize signal the main thread have to sleep.
374   std::this_thread::sleep_for(std::chrono::milliseconds(100));
375
376   application.SendNotification();
377   application.Render();
378
379   DALI_TEST_CHECK(true);
380   END_TEST;
381 }
382
383 int UtcDaliGlViewDirectRenderingTerminateCallback(void)
384 {
385   ToolkitTestApplication application;
386   tet_infoline("UtcDaliGlViewDirectRenderingTerminateCallback");
387   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
388
389   application.GetScene().Add(view);
390   view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
391   view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrame), Dali::MakeCallback(DirectRenderingCode::glTerminate));
392   view.SetResizeCallback(Dali::MakeCallback(DirectRenderingCode::resizeCB));
393   view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
394   view.SetProperty(Actor::Property::SIZE, Vector2(360.0f, 360.0f));
395
396   application.SendNotification();
397   application.Render();
398
399
400
401   //To GlViewRenderThread can recognize Resize signal the main thread have to sleep.
402   std::this_thread::sleep_for(std::chrono::milliseconds(100));
403
404   application.SendNotification();
405   application.Render();
406
407   DALI_TEST_CHECK(true);
408   END_TEST;
409 }
410
411 // Positive test case for a method
412 int UtcDaliGlViewDirectRenderingThreadedNew(void)
413 {
414   ToolkitTestApplication application;
415   tet_infoline(" UtcDaliGlViewDirectRenderingThreadedNew");
416   GlView view = GlView::New(GlView::BackendMode::DIRECT_RENDERING_THREADED, GlView::ColorFormat::RGBA8888);
417   DALI_TEST_CHECK(view);
418
419   auto mode1 = view.GetBackendMode();
420
421   DALI_TEST_EQUALS(mode1, GlView::BackendMode::DIRECT_RENDERING_THREADED, TEST_LOCATION);
422
423   END_TEST;
424 }
425
426 int UtcDaliGlViewDirectRenderingThreadedOnScene(void)
427 {
428   ToolkitTestApplication application;
429
430   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING_THREADED, GlView::ColorFormat::RGB888);
431
432   //Onscene
433   application.GetScene().Add(view);
434   view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
435   view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_3_0);
436   view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInitMT), Dali::MakeCallback(DirectRenderingCode::glRenderFrameMT), Dali::MakeCallback(DirectRenderingCode::glTerminateMT));
437   view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
438   view.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
439
440   // Set size on the actor (half the window size to show that glClear() and scissor test work together)
441   view.SetProperty(Actor::Property::SIZE, Size(100, 100));
442   view.SetProperty(Actor::Property::POSITION, Vector2(0,0));
443
444   while( DirectRenderingCode::gDRFramesRendered < 1 )
445   {
446     application.SendNotification();
447     application.Render();
448   }
449   DALI_TEST_CHECK(true);
450   END_TEST;
451 }
452
453 extern "C" bool gDirectRenderingFailCreateShader;
454 extern "C" bool gDirectRenderingFailCreateProgram;
455
456
457 int UtcDaliGlViewDirectRenderingThreadedOnScene1(void)
458 {
459   ToolkitTestApplication application;
460
461   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING_THREADED, GlView::ColorFormat::RGB888);
462
463   // This test will fail instantiating shaders
464   gDirectRenderingFailCreateShader = true;
465
466   //Onscene
467   application.GetScene().Add(view);
468   view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
469   view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_3_0);
470   view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInitMT), Dali::MakeCallback(DirectRenderingCode::glRenderFrameMT), Dali::MakeCallback(DirectRenderingCode::glTerminateMT));
471   view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
472   view.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
473
474   // Set size on the actor (half the window size to show that glClear() and scissor test work together)
475   view.SetProperty(Actor::Property::SIZE, Size(100, 100));
476   view.SetProperty(Actor::Property::POSITION, Vector2(0,0));
477
478   while( DirectRenderingCode::gDRFramesRendered < 1 )
479   {
480     application.SendNotification();
481     application.Render();
482   }
483   DALI_TEST_CHECK(true);
484   END_TEST;
485 }
486
487 int UtcDaliGlViewDirectRenderingThreadedOnScene2(void)
488 {
489   ToolkitTestApplication application;
490
491   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING_THREADED, GlView::ColorFormat::RGB888);
492
493   // This test will fail instantiating shaders
494   gDirectRenderingFailCreateProgram = true;
495
496   //Onscene
497   application.GetScene().Add(view);
498   view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
499   view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_3_0);
500   view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInitMT), Dali::MakeCallback(DirectRenderingCode::glRenderFrameMT), Dali::MakeCallback(DirectRenderingCode::glTerminateMT));
501   view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
502   view.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
503
504   // Set size on the actor (half the window size to show that glClear() and scissor test work together)
505   view.SetProperty(Actor::Property::SIZE, Size(100, 100));
506   view.SetProperty(Actor::Property::POSITION, Vector2(0,0));
507
508   while( DirectRenderingCode::gDRFramesRendered < 1 )
509   {
510     application.SendNotification();
511     application.Render();
512   }
513   DALI_TEST_CHECK(true);
514   END_TEST;
515 }