[dali_2.3.31] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / key-frame-channel.h
1 #ifndef DALI_INTERNAL_KEY_FRAME_CHANNEL_H
2 #define DALI_INTERNAL_KEY_FRAME_CHANNEL_H
3
4 /*
5  * Copyright (c) 2024 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/event/animation/progress-value.h>
26 #include <dali/public-api/animation/animation.h>
27 #include <dali/public-api/common/vector-wrapper.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 template<typename V>
34 struct KeyFrameChannel
35 {
36   using ProgressValues = std::vector<ProgressValue<V>>;
37
38   bool IsActive(float progress) const
39   {
40     if(!mValues.empty() && (progress >= mValues[0].GetProgress()))
41     {
42       return true;
43     }
44     return false;
45   }
46
47   V GetValue(float progress, Dali::Animation::Interpolation interpolation) const
48   {
49     V interpolatedV{};
50
51     if(progress >= mValues.back().GetProgress())
52     {
53       interpolatedV = mValues.back().GetValue();
54     }
55     else
56     {
57       // Find lowest element s.t. progress is greater than progress.
58       // So start->GetProgress() <= progress < end->GetProgress() is satisfied.
59       auto end   = std::lower_bound(mValues.begin(), mValues.end(), progress, [](const auto& element, const float& progress) { return element.GetProgress() <= progress; });
60       auto start = end - 1;
61
62       const bool validInterval = (end != mValues.end()) && (start->GetProgress() <= progress);
63
64       if(validInterval)
65       {
66         float frameProgress = (progress - start->GetProgress()) / (end->GetProgress() - start->GetProgress());
67         if(interpolation == Dali::Animation::LINEAR)
68         {
69           Interpolate(interpolatedV, start->GetValue(), end->GetValue(), frameProgress);
70         }
71         else
72         {
73           //Calculate prev and next values
74           V prev;
75           if(start != mValues.begin())
76           {
77             prev = (start - 1)->GetValue();
78           }
79           else
80           {
81             //Project next value through start point
82             prev = start->GetValue() + (start->GetValue() - (start + 1)->GetValue());
83           }
84
85           V next;
86           if(end != mValues.end() - 1)
87           {
88             next = (end + 1)->GetValue();
89           }
90           else
91           {
92             //Project prev value through end point
93             next = end->GetValue() + (end->GetValue() - (end - 1)->GetValue());
94           }
95
96           CubicInterpolate(interpolatedV, prev, start->GetValue(), end->GetValue(), next, frameProgress);
97         }
98       }
99     }
100     return interpolatedV;
101   }
102
103   bool OptimizeValuesLinear()
104   {
105     ProgressValues optimizedValues;
106     bool           optimized = false;
107
108     // Optimize works only if value has more or equal than 3 values.
109     if(mValues.size() < 3u)
110     {
111       return optimized;
112     }
113
114     auto iter = mValues.begin();
115     for(; iter + 1 != mValues.end();)
116     {
117       // Insert iter, which is the first value, or jter what we fail to ignore previous loops.
118       optimizedValues.push_back(*iter);
119       const float iterProgress = iter->GetProgress();
120
121       auto jter = iter + 1;
122
123       for(; jter + 1 != mValues.end();)
124       {
125         // Check whether we can ignore jter now.
126         const auto  kter         = jter + 1;
127         const float jterProgress = jter->GetProgress();
128         const float kterProgress = kter->GetProgress();
129
130         float frameProgress = (jterProgress - iterProgress) / (kterProgress - iterProgress);
131
132         // Interpolate with iter and kter.
133         // Check value between interpolatedV and jter->GetValue().
134         // If two values are similar, we can skip jter!
135         V interpolatedV{};
136         Interpolate(interpolatedV, iter->GetValue(), kter->GetValue(), frameProgress);
137
138         // TODO : We might need to find more good way to compare two values
139         if(Property::Value(interpolatedV) != Property::Value(jter->GetValue()))
140         {
141           break;
142         }
143
144         optimized = true;
145
146         // Keep checking for the next jter
147         ++jter;
148       }
149       iter = jter;
150     }
151
152     // Insert last value.
153     optimizedValues.push_back(*iter);
154
155     if(optimized)
156     {
157       mValues = std::move(optimizedValues);
158     }
159     return optimized;
160   }
161
162   ProgressValues mValues;
163 };
164
165 } // namespace Internal
166 } // namespace Dali
167
168 #endif // DALI_INTERNAL_KEY_FRAME_CHANNEL_H