[dali_2.3.19] Merge branch '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 using namespace Dali;
29 using namespace Dali::Toolkit;
30
31 // Positive test case for a method
32 int UtcDaliGlViewDirectRenderingNew(void)
33 {
34   ToolkitTestApplication application;
35   tet_infoline(" UtcDaliGlViewDirectRenderingNew");
36   GlView view = GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGBA8888);
37   DALI_TEST_CHECK(view);
38
39   auto mode1 = view.GetBackendMode();
40
41   DALI_TEST_EQUALS(mode1, GlView::BackendMode::DIRECT_RENDERING, TEST_LOCATION);
42
43   GlView view2 = GlView::New(GlView::BackendMode::EGL_IMAGE_OFFSCREEN_RENDERING, GlView::ColorFormat::RGBA8888);
44   DALI_TEST_CHECK(view2);
45
46   auto mode2 = view2.GetBackendMode();
47   DALI_TEST_EQUALS(mode2, GlView::BackendMode::EGL_IMAGE_OFFSCREEN_RENDERING, TEST_LOCATION);
48
49   END_TEST;
50 }
51
52 int UtcDaliGlViewDirectRenderingNewN(void)
53 {
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);
59
60   END_TEST;
61 }
62
63 // Positive test case for a method
64 int UtcDaliGlViewDirectRenderingDownCast(void)
65 {
66   ToolkitTestApplication application;
67   tet_infoline(" UtcDaliGlViewDirectRenderingDownCast");
68
69   GlView     view = GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
70   BaseHandle handle(view);
71
72   Toolkit::GlView view2 = Toolkit::GlView::DownCast(handle);
73   DALI_TEST_CHECK(view);
74   DALI_TEST_CHECK(view2);
75   DALI_TEST_CHECK(view == view2);
76   END_TEST;
77 }
78
79 int UtcDaliGlViewDirectRenderingCopyAndAssignment(void)
80 {
81   ToolkitTestApplication application;
82   tet_infoline("UtcDaliGlViewDirectRenderingCopyAndAssignment");
83
84   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
85   DALI_TEST_CHECK(view);
86
87   GlView copy(view);
88   DALI_TEST_CHECK(view == copy);
89
90   GlView assign;
91   DALI_TEST_CHECK(!assign);
92
93   assign = copy;
94   DALI_TEST_CHECK(assign == view);
95
96   END_TEST;
97 }
98
99 int UtcDaliGlViewDirectRenderingMoveAssignment(void)
100 {
101   ToolkitTestApplication application;
102   tet_infoline("UtcDaliGlViewDirectRenderingMoveAssignment");
103
104   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
105   DALI_TEST_EQUALS(1, view.GetBaseObject().ReferenceCount(), TEST_LOCATION);
106
107   GlView moved;
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);
112
113   END_TEST;
114 }
115
116 int UtcDaliGlViewDirectRenderingSetGraphicsConfigGles20N(void)
117 {
118   ToolkitTestApplication application;
119   tet_infoline("UtcDaliGlViewDirectRenderingSetGraphicsConfigGles20");
120   GlView view;
121   try
122   {
123     view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
124     DALI_TEST_CHECK(false);
125   }
126   catch(...)
127   {
128     DALI_TEST_CHECK(true);
129   }
130   END_TEST;
131 }
132
133 int UtcDaliGlViewDirectRenderingSetGraphicsConfigGles30(void)
134 {
135   ToolkitTestApplication application;
136   tet_infoline("UtcDaliGlViewDirectRenderingSetGraphicsConfigGles30");
137   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
138
139   try
140   {
141     view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_3_0);
142     DALI_TEST_CHECK(true);
143   }
144   catch(...)
145   {
146     DALI_TEST_CHECK(false);
147   }
148   END_TEST;
149 }
150
151 int UtcDaliGlViewDirectRenderingRenderingMode(void)
152 {
153   ToolkitTestApplication application;
154   tet_infoline("UtcDaliGlViewDirectRenderingRenderingMode");
155   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
156
157   view.SetRenderingMode(GlView::RenderingMode::ON_DEMAND);
158
159   GlView::RenderingMode mode = view.GetRenderingMode();
160
161   DALI_TEST_EQUALS(GlView::RenderingMode::ON_DEMAND, mode, TEST_LOCATION);
162
163   END_TEST;
164 }
165
166 int UtcDaliGlViewDirectRenderingOnSizeSet(void)
167 {
168   ToolkitTestApplication application;
169   tet_infoline("UtcDaliGlViewDirectRenderingOnSizeSet");
170   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
171
172   application.GetScene().Add(view);
173
174   application.SendNotification();
175   application.Render();
176
177   Vector3 size(200.0f, 300.0f, 0.0f);
178   view.SetProperty(Actor::Property::SIZE, size);
179
180   application.SendNotification();
181   application.Render();
182
183   DALI_TEST_EQUALS(view.GetCurrentProperty<Vector3>(Actor::Property::SIZE), size, TEST_LOCATION);
184
185   END_TEST;
186 }
187
188 namespace DirectRenderingCode
189 {
190 // Internal callback function
191 void glInit(Dali::RenderCallbackInput& input)
192 {
193 }
194
195 int glRenderFrame(Dali::RenderCallbackInput& input)
196 {
197   static unsigned int retFlag = 0;
198   return retFlag++;
199 }
200
201 int gBoundTextureCount = 0;
202
203 int glRenderFrameWithTextures(Dali::RenderCallbackInput& input)
204 {
205   gBoundTextureCount = input.textureBindings.size();
206   return 1;
207 }
208
209 void glTerminate(Dali::RenderCallbackInput& input)
210 {
211 }
212
213 // Internal callback function
214 void glInitMT(Dali::RenderCallbackInput& input)
215 {
216 }
217
218 int gDRFramesRendered = 0;
219
220 int glRenderFrameMT(Dali::RenderCallbackInput& input)
221 {
222   gDRFramesRendered++;
223   return 1;
224 }
225
226 void glTerminateMT(Dali::RenderCallbackInput& input)
227 {
228 }
229
230 void resizeCB(Vector2 size)
231 {
232 }
233
234 } // namespace DirectRenderingCode
235
236 int UtcDaliGlViewDirectRenderingRegisterGlCallbacksN(void)
237 {
238   ToolkitTestApplication application;
239   tet_infoline("UtcDaliGlViewDirectRenderingRegisterGlCallbacksN");
240   GlView view;
241
242   try
243   {
244     view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrame), Dali::MakeCallback(DirectRenderingCode::glTerminate));
245     DALI_TEST_CHECK(false);
246   }
247   catch(...)
248   {
249     DALI_TEST_CHECK(true);
250   }
251   END_TEST;
252 }
253
254 int UtcDaliGlViewDirectRenderingSetResizeCallbackN(void)
255 {
256   ToolkitTestApplication application;
257   tet_infoline("UtcDaliGlViewDirectRenderingSetResizeCallback");
258   GlView view;
259
260   try
261   {
262     view.SetResizeCallback(Dali::MakeCallback(DirectRenderingCode::resizeCB));
263     DALI_TEST_CHECK(false);
264   }
265   catch(...)
266   {
267     DALI_TEST_CHECK(true);
268   }
269   END_TEST;
270 }
271
272 int UtcDaliGlViewDirectRenderingRenderOnce(void)
273 {
274   ToolkitTestApplication application;
275   tet_infoline("UtcDaliGlViewDirectRenderingRenderOnce");
276   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
277
278   try
279   {
280     view.RenderOnce();
281     DALI_TEST_CHECK(true);
282   }
283   catch(...)
284   {
285     DALI_TEST_CHECK(false);
286   }
287   END_TEST;
288 }
289
290 int UtcDaliGlViewDirectRenderingWindowVisibilityChanged(void)
291 {
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));
300
301   application.SendNotification();
302   application.Render();
303
304   Window window = DevelWindow::Get(view);
305   window.Hide();
306   application.SendNotification();
307   application.Render();
308
309   DALI_TEST_CHECK(true);
310   END_TEST;
311 }
312
313 int UtcDaliGlViewDirectRenderingOnScene(void)
314 {
315   ToolkitTestApplication application;
316
317   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
318
319   //Onscene
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));
324
325   application.SendNotification();
326   application.Render();
327
328   //Offscene
329   application.GetScene().Remove(view);
330
331   application.SendNotification();
332   application.Render();
333
334   DALI_TEST_CHECK(true);
335   END_TEST;
336 }
337
338 int UtcDaliGlViewDirectRenderingControlVisibilityChanged(void)
339 {
340   ToolkitTestApplication application;
341
342   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
343   application.GetScene().Add(view);
344
345   application.SendNotification();
346   application.Render();
347
348   view.SetProperty(Actor::Property::VISIBLE, false);
349   application.SendNotification();
350   application.Render();
351   DALI_TEST_CHECK(view.GetCurrentProperty<bool>(Actor::Property::VISIBLE) == false);
352
353   view.SetProperty(Actor::Property::VISIBLE, true);
354   application.SendNotification();
355   application.Render();
356   DALI_TEST_CHECK(view.GetCurrentProperty<bool>(Actor::Property::VISIBLE) == true);
357
358   END_TEST;
359 }
360
361 int UtcDaliGlViewDirectRenderingResize(void)
362 {
363   ToolkitTestApplication application;
364   tet_infoline("UtcDaliGlViewDirectRenderingResize");
365   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
366
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));
373
374   application.SendNotification();
375   application.Render();
376
377   //To GlViewRenderThread can recognize Resize signal the main thread have to sleep.
378   std::this_thread::sleep_for(std::chrono::milliseconds(100));
379
380   application.SendNotification();
381   application.Render();
382
383   DALI_TEST_CHECK(true);
384   END_TEST;
385 }
386
387 int UtcDaliGlViewDirectRenderingTerminateCallback(void)
388 {
389   ToolkitTestApplication application;
390   tet_infoline("UtcDaliGlViewDirectRenderingTerminateCallback");
391   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
392
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));
399
400   application.SendNotification();
401   application.Render();
402
403   //To GlViewRenderThread can recognize Resize signal the main thread have to sleep.
404   std::this_thread::sleep_for(std::chrono::milliseconds(100));
405
406   application.SendNotification();
407   application.Render();
408
409   DALI_TEST_CHECK(true);
410   END_TEST;
411 }
412
413 int UtcDaliGlViewDirectRenderingTextureBinding(void)
414 {
415   ToolkitTestApplication application;
416
417   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING, GlView::ColorFormat::RGB888);
418
419   view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
420   view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_2_0);
421   view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInit), Dali::MakeCallback(DirectRenderingCode::glRenderFrameWithTextures), Dali::MakeCallback(DirectRenderingCode::glTerminate));
422   view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
423   view.SetProperty(Actor::Property::SIZE, Vector2(360.0f, 360.0f));
424
425   // Set size on the actor (half the window size to show that glClear() and scissor test work together)
426   view.SetProperty(Actor::Property::SIZE, Size(100, 100));
427   view.SetProperty(Actor::Property::POSITION, Vector2(0, 0));
428
429   application.GetScene().Add(view);
430
431   // Prepare texture 1
432   Texture   texture1   = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::Format::RGBA8888, 512, 512);
433   auto*     data1      = reinterpret_cast<uint8_t*>(malloc(512 * 512 * 4));
434   PixelData pixelData1 = PixelData::New(data1, 512 * 512 * 4, 512, 512, Pixel::Format::RGBA8888, PixelData::ReleaseFunction::FREE);
435   texture1.Upload(pixelData1);
436
437   // Prepare texture 2
438   Texture   texture2   = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::Format::RGBA8888, 512, 512);
439   auto*     data2      = reinterpret_cast<uint8_t*>(malloc(512 * 512 * 4));
440   PixelData pixelData2 = PixelData::New(data2, 512 * 512 * 4, 512, 512, Pixel::Format::RGBA8888, PixelData::ReleaseFunction::FREE);
441   texture2.Upload(pixelData2);
442
443   std::vector<Texture> texturesToBind;
444   texturesToBind.push_back(texture1);
445   texturesToBind.push_back(texture2);
446
447   view.BindTextureResources(texturesToBind);
448
449   DirectRenderingCode::gBoundTextureCount = 0;
450
451   application.SendNotification();
452   application.Render();
453
454   DALI_TEST_EQUALS(DirectRenderingCode::gBoundTextureCount, texturesToBind.size(), TEST_LOCATION);
455
456   END_TEST;
457 }
458
459 // Positive test case for a method
460 int UtcDaliGlViewDirectRenderingThreadedNew(void)
461 {
462   ToolkitTestApplication application;
463   tet_infoline(" UtcDaliGlViewDirectRenderingThreadedNew");
464   GlView view = GlView::New(GlView::BackendMode::DIRECT_RENDERING_THREADED, GlView::ColorFormat::RGBA8888);
465   DALI_TEST_CHECK(view);
466
467   auto mode1 = view.GetBackendMode();
468
469   DALI_TEST_EQUALS(mode1, GlView::BackendMode::DIRECT_RENDERING_THREADED, TEST_LOCATION);
470
471   END_TEST;
472 }
473
474 int UtcDaliGlViewDirectRenderingThreadedOnScene(void)
475 {
476   ToolkitTestApplication application;
477
478   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING_THREADED, GlView::ColorFormat::RGB888);
479
480   //Onscene
481   application.GetScene().Add(view);
482   view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
483   view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_3_0);
484   view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInitMT), Dali::MakeCallback(DirectRenderingCode::glRenderFrameMT), Dali::MakeCallback(DirectRenderingCode::glTerminateMT));
485   view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
486   view.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
487
488   // Set size on the actor (half the window size to show that glClear() and scissor test work together)
489   view.SetProperty(Actor::Property::SIZE, Size(100, 100));
490   view.SetProperty(Actor::Property::POSITION, Vector2(0, 0));
491
492   while(DirectRenderingCode::gDRFramesRendered < 1)
493   {
494     application.SendNotification();
495     application.Render();
496   }
497   DALI_TEST_CHECK(true);
498   END_TEST;
499 }
500
501 extern "C" bool gDirectRenderingFailCreateShader;
502 extern "C" bool gDirectRenderingFailCreateProgram;
503
504 int UtcDaliGlViewDirectRenderingThreadedOnScene1(void)
505 {
506   ToolkitTestApplication application;
507
508   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING_THREADED, GlView::ColorFormat::RGB888);
509
510   // This test will fail instantiating shaders
511   gDirectRenderingFailCreateShader = true;
512
513   //Onscene
514   application.GetScene().Add(view);
515   view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
516   view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_3_0);
517   view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInitMT), Dali::MakeCallback(DirectRenderingCode::glRenderFrameMT), Dali::MakeCallback(DirectRenderingCode::glTerminateMT));
518   view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
519   view.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
520
521   // Set size on the actor (half the window size to show that glClear() and scissor test work together)
522   view.SetProperty(Actor::Property::SIZE, Size(100, 100));
523   view.SetProperty(Actor::Property::POSITION, Vector2(0, 0));
524
525   while(DirectRenderingCode::gDRFramesRendered < 1)
526   {
527     application.SendNotification();
528     application.Render();
529   }
530   DALI_TEST_CHECK(true);
531   END_TEST;
532 }
533
534 int UtcDaliGlViewDirectRenderingThreadedOnScene2(void)
535 {
536   ToolkitTestApplication application;
537
538   GlView view = Toolkit::GlView::New(GlView::BackendMode::DIRECT_RENDERING_THREADED, GlView::ColorFormat::RGB888);
539
540   // This test will fail instantiating shaders
541   gDirectRenderingFailCreateProgram = true;
542
543   //Onscene
544   application.GetScene().Add(view);
545   view.SetRenderingMode(GlView::RenderingMode::CONTINUOUS);
546   view.SetGraphicsConfig(true, true, 0, GlView::GraphicsApiVersion::GLES_VERSION_3_0);
547   view.RegisterGlCallbacks(Dali::MakeCallback(DirectRenderingCode::glInitMT), Dali::MakeCallback(DirectRenderingCode::glRenderFrameMT), Dali::MakeCallback(DirectRenderingCode::glTerminateMT));
548   view.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
549   view.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
550
551   // Set size on the actor (half the window size to show that glClear() and scissor test work together)
552   view.SetProperty(Actor::Property::SIZE, Size(100, 100));
553   view.SetProperty(Actor::Property::POSITION, Vector2(0, 0));
554
555   while(DirectRenderingCode::gDRFramesRendered < 1)
556   {
557     application.SendNotification();
558     application.Render();
559   }
560   DALI_TEST_CHECK(true);
561   END_TEST;
562 }