(PanGesture) Logging enabled via environment variable
[platform/core/uifw/dali-core.git] / dali / internal / update / gestures / scene-graph-pan-gesture.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/update/gestures/scene-graph-pan-gesture.h>
19
20 // EXTERNAL INCLUDES
21
22 // INTERNAL INCLUDES
23 #include <dali/internal/update/gestures/pan-gesture-profiling.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 namespace SceneGraph
32 {
33
34 namespace
35 {
36
37 const unsigned int ARRAY_SIZE( 4u );
38
39 const unsigned int UPDATES_BETWEEN_PRINT( 120u );
40 unsigned int UPDATE_COUNT( 0u );
41
42 } // unnamed namespace
43
44 PanGesture* PanGesture::New()
45 {
46   return new PanGesture();
47 }
48
49 PanGesture::~PanGesture()
50 {
51   delete mProfiling;
52 }
53
54 void PanGesture::AddGesture( const Dali::PanGesture& gesture )
55 {
56   mGestures[ mWritePosition ] = gesture;
57
58   // Update our write position (so now this new value can be read)
59   // Note: we want to overwrite oldest gesture information even if it hasn't been read.
60   ++mWritePosition;
61   mWritePosition %= ARRAY_SIZE;
62 }
63
64 void PanGesture::UpdateProperties( unsigned int lastVSyncTime, unsigned int nextVSyncTime )
65 {
66   unsigned int time( 0u );
67   bool justStarted ( false );
68   bool justFinished ( false );
69
70   // If our read position is not same as write position, then there
71   // must be new values on circular buffer to read.
72   while(mReadPosition != mWritePosition)
73   {
74     const PanInfo& currentGesture = mGestures[mReadPosition];
75
76     if( mProfiling )
77     {
78       mProfiling->mRawData.push_back( PanGestureProfiling::Position( currentGesture.time, currentGesture.screen.position ) );
79     }
80
81     if ( currentGesture.time > time )
82     {
83       justStarted |= (currentGesture.state == Gesture::Started);
84       justFinished |= (currentGesture.state == Gesture::Finished || currentGesture.state == Gesture::Cancelled);
85
86       // use position values direct from gesture.
87       mLatestGesture.screen.position = currentGesture.screen.position;
88       mLatestGesture.local.position = currentGesture.local.position;
89
90       // change displacement to be relative to initial touch, instead of relative to last touch-frame.
91       if(currentGesture.state == Gesture::Started)
92       {
93         mLatestGesture.screen.displacement = currentGesture.screen.displacement;
94         mLatestGesture.local.displacement = currentGesture.local.displacement;
95       }
96       else
97       {
98         mLatestGesture.screen.displacement += currentGesture.screen.displacement;
99         mLatestGesture.local.displacement += currentGesture.local.displacement;
100       }
101
102       time = currentGesture.time;
103     }
104
105     ++mReadPosition;
106     mReadPosition %= ARRAY_SIZE;
107   }
108
109   mInGesture |= justStarted;
110
111   if ( mInGesture )
112   {
113     PanInfo gesture(mLatestGesture);
114
115     if( mProfiling )
116     {
117       mProfiling->mLatestData.push_back( PanGestureProfiling::Position( lastVSyncTime, gesture.screen.position ) );
118     }
119
120     if( !justStarted ) // only use previous frame if this is the continuing.
121     {
122       // If previous gesture exists, then produce position as 50/50 interpolated blend of these two points.
123       gesture.screen.position += mPreviousGesture.screen.position;
124       gesture.local.position += mPreviousGesture.local.position;
125       gesture.screen.position *= 0.5f;
126       gesture.local.position *= 0.5f;
127       // make current displacement relative to previous update-frame now.
128       gesture.screen.displacement -= mPreviousGesture.screen.displacement;
129       gesture.local.displacement -= mPreviousGesture.local.displacement;
130     }
131
132     if( mProfiling )
133     {
134       mProfiling->mAveragedData.push_back( PanGestureProfiling::Position( lastVSyncTime, gesture.screen.position ) );
135     }
136
137     mPreviousGesture = mLatestGesture;
138
139     mScreenPosition.Set( gesture.screen.position );
140     mScreenDisplacement.Set( gesture.screen.displacement );
141     mLocalPosition.Set( gesture.local.position );
142     mLocalDisplacement.Set( gesture.local.displacement );
143   }
144
145   mInGesture &= ~justFinished;
146
147   if( mProfiling &&
148       UPDATE_COUNT++ >= UPDATES_BETWEEN_PRINT )
149   {
150     mProfiling->PrintData();
151     mProfiling->ClearData();
152     UPDATE_COUNT = 0u;
153   }
154 }
155
156 const GesturePropertyVector2& PanGesture::GetScreenPositionProperty() const
157 {
158   return mScreenPosition;
159 }
160
161 const GesturePropertyVector2& PanGesture::GetScreenDisplacementProperty() const
162 {
163   return mScreenDisplacement;
164 }
165
166 const GesturePropertyVector2& PanGesture::GetLocalPositionProperty() const
167 {
168   return mLocalPosition;
169 }
170
171 const GesturePropertyVector2& PanGesture::GetLocalDisplacementProperty() const
172 {
173   return mLocalDisplacement;
174 }
175
176 void PanGesture::EnableProfiling()
177 {
178   if( !mProfiling )
179   {
180     mProfiling = new PanGestureProfiling();
181   }
182 }
183
184 void PanGesture::ResetDefaultProperties( BufferIndex updateBufferIndex )
185 {
186   mScreenPosition.Reset();
187   mScreenDisplacement.Reset();
188   mLocalPosition.Reset();
189   mLocalDisplacement.Reset();
190 }
191
192 PanGesture::PanGesture()
193 : mGestures(),
194   mWritePosition( 0 ),
195   mReadPosition( 0 ),
196   mInGesture( false ),
197   mProfiling( NULL )
198 {
199 }
200
201 } // namespace SceneGraph
202
203 } // namespace Internal
204
205 } // namespace Dali