Benchmark demos
[platform/core/uifw/dali-demo.git] / examples / tl-benchmark / text-label-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                                  = TextLabel::New(Control::ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS);
115           bgView[TextLabel::Property::TEXT]       = "Hello, world!";
116           bgView[TextLabel::Property::POINT_SIZE] = 12;
117         }
118         customNew1Logger.AddMarker(PerformanceLogger::Marker::END_EVENT);
119
120         customColorLogger.AddMarker(PerformanceLogger::Marker::START_EVENT);
121         bgView.SetBackgroundColor(Color::CRIMSON);
122         customColorLogger.AddMarker(PerformanceLogger::Marker::END_EVENT);
123         bgView[Actor::Property::PARENT_ORIGIN] = ParentOrigin::TOP_LEFT;
124         bgView[Actor::Property::ANCHOR_POINT]  = AnchorPoint::TOP_LEFT;
125         bgView[Actor::Property::SIZE]          = Vector2(width * 0.9f, height * 0.9f);
126         bgView[Actor::Property::POSITION]      = Vector2(width * j + width * 0.05f, height * 0.05f);
127         rawView.Add(bgView);
128       }
129       customLoopLogger.AddMarker(PerformanceLogger::Marker::END_EVENT);
130
131       window.GetRootLayer().Add(rawView);
132       list.push_back(rawView);
133
134       Animation animation = Animation::New(duration * 0.001f * n);
135       animation.AnimateTo(Property(rawView, Actor::Property::POSITION_Y), -height);
136       animation.Play();
137
138       while((int)list.size() > n)
139       {
140         list.front().Unparent();
141         list.pop_front();
142       }
143     }
144
145     return true;
146   }
147
148   void OnKeyEvent(const KeyEvent& event)
149   {
150     if(event.GetState() == KeyEvent::DOWN)
151     {
152       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
153       {
154         timer.Stop();
155         list.clear();
156         mApplication.Quit();
157       }
158     }
159   }
160
161 private:
162   Application& mApplication;
163 };
164
165 int DALI_EXPORT_API main(int argc, char** argv)
166 {
167   Application        application = Application::New(&argc, &argv);
168   ColorVisualExample test(application);
169   application.MainLoop();
170   return 0;
171 }