Add BUILD_EXAMPLE_NAME option explain for README.md
[platform/core/uifw/dali-demo.git] / examples / iv-benchmark / image-visual-benchmark.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 <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/control-devel.h>
20 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
21 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
22 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
23 #include <dali/integration-api/debug.h>
24
25 #include <dali/devel-api/adaptor-framework/performance-logger.h>
26
27 #include <chrono>
28 #include <list>
29 #include <thread>
30
31 using namespace Dali;
32 using namespace Dali::Toolkit;
33
34 namespace
35 {
36 } // namespace
37
38 // This example shows the blur radius property of the color visual and animates it.
39 //
40 class ColorVisualExample : public ConnectionTracker
41 {
42 public:
43   ColorVisualExample(Application& application)
44   : mApplication(application)
45   {
46     // Connect to the Application's Init signal
47     mApplication.InitSignal().Connect(this, &ColorVisualExample::Create);
48   }
49
50   ~ColorVisualExample()
51   {
52     // Nothing to do here;
53   }
54
55   Window             window;
56   Timer              timer;
57   std::list<Control> list;
58   const int          n        = 20;
59   const int          m        = 20;
60   const int          duration = 100; // miliseconds.
61
62   PerformanceLogger customLoopLogger;
63   PerformanceLogger customNew1Logger;
64   PerformanceLogger customColorLogger;
65
66   // The Init signal is received once (only) during the Application lifetime
67   void Create(Application& application)
68   {
69     StyleManager instance = StyleManager::Get();
70     window                = application.GetWindow();
71     window.SetBackgroundColor(Color::WHITE);
72
73     timer = Timer::New(duration);
74     timer.TickSignal().Connect(this, &ColorVisualExample::OnTick);
75     timer.Start();
76
77     customLoopLogger  = PerformanceLogger::New("20Controls");
78     customNew1Logger  = PerformanceLogger::New("1_New");
79     customColorLogger = PerformanceLogger::New("2_SetBG");
80
81     customLoopLogger.EnableLogging(true);
82     customNew1Logger.EnableLogging(true);
83     customColorLogger.EnableLogging(true);
84
85     // Respond to key events
86     window.KeyEventSignal().Connect(this, &ColorVisualExample::OnKeyEvent);
87   }
88
89   bool OnTick()
90   {
91     float width  = (float)window.GetSize().GetWidth() / m;
92     float height = (float)window.GetSize().GetHeight() / n;
93
94     int i = n - 1;
95     {
96       customLoopLogger.AddMarker(PerformanceLogger::Marker::START_EVENT);
97       customNew1Logger.AddMarker(PerformanceLogger::Marker::START_EVENT);
98       Control rawView = Control::New(Control::ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS);
99       customNew1Logger.AddMarker(PerformanceLogger::Marker::END_EVENT);
100
101       customColorLogger.AddMarker(PerformanceLogger::Marker::START_EVENT);
102       rawView.SetBackgroundColor(Color::BLUE);
103       customColorLogger.AddMarker(PerformanceLogger::Marker::END_EVENT);
104       rawView[Actor::Property::PARENT_ORIGIN] = ParentOrigin::TOP_LEFT;
105       rawView[Actor::Property::ANCHOR_POINT]  = AnchorPoint::TOP_LEFT;
106       rawView[Actor::Property::SIZE]          = Vector2((float)window.GetSize().GetWidth(), height);
107       rawView[Actor::Property::POSITION]      = Vector2(0.0f, height * i);
108
109       for(int j = 0; j < m; j++)
110       {
111         Control bgView;
112         customNew1Logger.AddMarker(PerformanceLogger::Marker::START_EVENT);
113         {
114           bgView = ImageView::New(Control::ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS, DEMO_IMAGE_DIR "gallery-small-23.jpg");
115         }
116         customNew1Logger.AddMarker(PerformanceLogger::Marker::END_EVENT);
117
118         customColorLogger.AddMarker(PerformanceLogger::Marker::START_EVENT);
119         bgView.SetBackgroundColor(Color::CRIMSON);
120         customColorLogger.AddMarker(PerformanceLogger::Marker::END_EVENT);
121         bgView[Actor::Property::PARENT_ORIGIN] = ParentOrigin::TOP_LEFT;
122         bgView[Actor::Property::ANCHOR_POINT]  = AnchorPoint::TOP_LEFT;
123         bgView[Actor::Property::SIZE]          = Vector2(width * 0.9f, height * 0.9f);
124         bgView[Actor::Property::POSITION]      = Vector2(width * j + width * 0.05f, height * 0.05f);
125         rawView.Add(bgView);
126       }
127       customLoopLogger.AddMarker(PerformanceLogger::Marker::END_EVENT);
128
129       window.GetRootLayer().Add(rawView);
130       list.push_back(rawView);
131
132       Animation animation = Animation::New(duration * 0.001f * n);
133       animation.AnimateTo(Property(rawView, Actor::Property::POSITION_Y), -height);
134       animation.Play();
135
136       while((int)list.size() > n)
137       {
138         list.front().Unparent();
139         list.pop_front();
140       }
141     }
142
143     return true;
144   }
145
146   void OnKeyEvent(const KeyEvent& event)
147   {
148     if(event.GetState() == KeyEvent::DOWN)
149     {
150       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
151       {
152         timer.Stop();
153         list.clear();
154         mApplication.Quit();
155       }
156     }
157   }
158
159 private:
160   Application& mApplication;
161 };
162
163 int DALI_EXPORT_API main(int argc, char** argv)
164 {
165   Application        application = Application::New(&argc, &argv);
166   ColorVisualExample test(application);
167   application.MainLoop();
168   return 0;
169 }