Merge "Automatic image atlasing" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-application.cpp
1 /*
2  * Copyright (c) 2014 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 "test-application.h"
19
20 namespace Dali
21 {
22
23
24 TestApplication::TestApplication( size_t surfaceWidth,
25                                   size_t surfaceHeight,
26                                   float  horizontalDpi,
27                                   float  verticalDpi,
28                                   ResourcePolicy::DataRetention policy)
29 : mCore( NULL ),
30   mSurfaceWidth( surfaceWidth ),
31   mSurfaceHeight( surfaceHeight ),
32   mFrame( 0u ),
33   mDpi( horizontalDpi, verticalDpi ),
34   mLastVSyncTime(0u),
35   mDataRetentionPolicy( policy )
36 {
37   Initialize();
38 }
39
40 TestApplication::TestApplication( bool   initialize,
41                                   size_t surfaceWidth,
42                                   size_t surfaceHeight,
43                                   float  horizontalDpi,
44                                   float  verticalDpi,
45                                   ResourcePolicy::DataRetention policy)
46 : mCore( NULL ),
47   mSurfaceWidth( surfaceWidth ),
48   mSurfaceHeight( surfaceHeight ),
49   mFrame( 0u ),
50   mDpi( horizontalDpi, verticalDpi ),
51   mDataRetentionPolicy( policy )
52 {
53   if ( initialize )
54   {
55     Initialize();
56   }
57 }
58
59 void TestApplication::Initialize()
60 {
61   mCore = Dali::Integration::Core::New(
62     mRenderController,
63     mPlatformAbstraction,
64     mGlAbstraction,
65     mGlSyncAbstraction,
66     mGestureManager,
67     mDataRetentionPolicy);
68
69   mCore->ContextCreated();
70   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
71   mCore->SetDpi( mDpi.x, mDpi.y );
72
73   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
74   Dali::Integration::Log::InstallLogFunction(logFunction);
75
76   mCore->SceneCreated();
77 }
78
79 TestApplication::~TestApplication()
80 {
81   Dali::Integration::Log::UninstallLogFunction();
82   delete mCore;
83 }
84
85 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
86 {
87   switch(level)
88   {
89     case Dali::Integration::Log::DebugInfo:
90       fprintf(stderr, "INFO: %s", message.c_str());
91       break;
92     case Dali::Integration::Log::DebugWarning:
93       fprintf(stderr, "WARN: %s", message.c_str());
94       break;
95     case Dali::Integration::Log::DebugError:
96       fprintf(stderr, "ERROR: %s", message.c_str());
97       break;
98     default:
99       fprintf(stderr, "DEFAULT: %s", message.c_str());
100       break;
101   }
102 }
103
104 Dali::Integration::Core& TestApplication::GetCore()
105 {
106   return *mCore;
107 }
108
109 TestPlatformAbstraction& TestApplication::GetPlatform()
110 {
111   return mPlatformAbstraction;
112 }
113
114 TestRenderController& TestApplication::GetRenderController()
115 {
116   return mRenderController;
117 }
118
119 TestGlAbstraction& TestApplication::GetGlAbstraction()
120 {
121   return mGlAbstraction;
122 }
123
124 TestGlSyncAbstraction& TestApplication::GetGlSyncAbstraction()
125 {
126   return mGlSyncAbstraction;
127 }
128
129 TestGestureManager& TestApplication::GetGestureManager()
130 {
131   return mGestureManager;
132 }
133
134 void TestApplication::ProcessEvent(const Integration::Event& event)
135 {
136   mCore->QueueEvent(event);
137   mCore->ProcessEvents();
138 }
139
140 void TestApplication::SendNotification()
141 {
142   mCore->ProcessEvents();
143 }
144
145 void TestApplication::SetSurfaceWidth( unsigned int width, unsigned height )
146 {
147   mSurfaceWidth = width;
148   mSurfaceHeight = height;
149
150   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
151 }
152
153 void TestApplication::DoUpdate( unsigned int intervalMilliseconds )
154 {
155   uint64_t seconds(0u), nanoseconds(0u);
156   mPlatformAbstraction.GetTimeNanoseconds( seconds, nanoseconds );
157   mLastVSyncTime = ( seconds * 1e3 ) + ( nanoseconds / 1e6 );
158   unsigned int nextVSyncTime = mLastVSyncTime + 16;
159
160   // Update Time values
161   mPlatformAbstraction.IncrementGetTimeResult( intervalMilliseconds );
162
163   float elapsedSeconds = intervalMilliseconds / 1e3f;
164   mCore->Update( elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus );
165 }
166
167 bool TestApplication::Render( unsigned int intervalMilliseconds  )
168 {
169   DoUpdate( intervalMilliseconds );
170   mCore->Render( mRenderStatus );
171
172   mFrame++;
173
174   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
175 }
176
177 unsigned int TestApplication::GetUpdateStatus()
178 {
179   return mStatus.KeepUpdating();
180 }
181
182 bool TestApplication::UpdateOnly( unsigned int intervalMilliseconds  )
183 {
184   DoUpdate( intervalMilliseconds );
185   return mStatus.KeepUpdating();
186 }
187
188 bool TestApplication::RenderOnly( )
189 {
190   // Update Time values
191   mCore->Render( mRenderStatus );
192
193   mFrame++;
194
195   return mRenderStatus.NeedsUpdate();
196 }
197
198 void TestApplication::ResetContext()
199 {
200   mCore->ContextDestroyed();
201   mCore->ContextCreated();
202 }
203
204
205 } // Namespace dali