[dali_2.1.33] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-vector-animation-renderer.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/devel-api/adaptor-framework/pixel-buffer.h>
19 #include <dali/devel-api/adaptor-framework/vector-animation-renderer.h>
20 #include <dali/devel-api/threading/mutex.h>
21 #include <dali/public-api/object/base-object.h>
22 #include <toolkit-application.h>
23 #include <toolkit-event-thread-callback.h>
24 #include <toolkit-vector-animation-renderer.h>
25 #include <chrono>
26 #include <memory>
27 #include <thread>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 namespace Adaptor
34 {
35 namespace
36 {
37 Dali::Internal::Adaptor::VectorAnimationRenderer* gVectorAnimationRenderer = nullptr;
38 }
39
40 class VectorAnimationRenderer : public Dali::BaseObject
41 {
42 public:
43   VectorAnimationRenderer()
44   : mUrl(),
45     mRenderer(),
46     mWidth(0),
47     mHeight(0),
48     mDefaultWidth(0),
49     mDefaultHeight(0),
50     mTotalFrameNumber(VECTOR_ANIMATION_TOTAL_FRAME_NUMBER),
51     mPreviousFrame(0),
52     mDelayTime(0),
53     mDroppedFrames(0),
54     mFrameRate(60.0f),
55     mTestFrameDrop(false),
56     mNeedDroppedFrames(false),
57     mEventThreadCallback(new EventThreadCallback(MakeCallback(this, &VectorAnimationRenderer::OnTriggered)))
58   {
59     mCount++;
60
61     if(mCount == 2)
62     {
63       mFrameRate = 0.1f;
64     }
65   }
66
67   ~VectorAnimationRenderer()
68   {
69     mCount--;
70   }
71
72   bool Load(const std::string& url)
73   {
74     Dali::Mutex::ScopedLock lock(mMutex);
75     mUrl = url;
76     if(mUrl == "invalid.json")
77     {
78       mLoadFailed = true;
79       return false;
80     }
81     else if(mUrl == "framedrop.json")
82     {
83       // Change total frame number for test
84       mTotalFrameNumber = 200;
85     }
86
87     mDefaultWidth  = 100;
88     mDefaultHeight = 100;
89
90     return true;
91   }
92
93   void SetRenderer(Dali::Renderer renderer)
94   {
95     mRenderer = renderer;
96   }
97
98   void SetSize(uint32_t width, uint32_t height)
99   {
100     Dali::Mutex::ScopedLock lock(mMutex);
101     mWidth  = width;
102     mHeight = height;
103
104     if(!mLoadFailed)
105     {
106       mNeedTrigger   = true;
107       mResourceReady = false;
108     }
109   }
110
111   bool Render(uint32_t frameNumber)
112   {
113     Dali::Mutex::ScopedLock lock(mMutex);
114     if(mWidth == 0 || mHeight == 0)
115     {
116       return false;
117     }
118
119     if(mTestFrameDrop)
120     {
121       std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int32_t>(mDelayTime)));
122       mTestFrameDrop     = false;
123       mNeedDroppedFrames = true;
124     }
125     else if(mNeedDroppedFrames)
126     {
127       mDroppedFrames     = (frameNumber > mPreviousFrame) ? frameNumber - mPreviousFrame - 1 : frameNumber + (mTotalFrameNumber - mPreviousFrame) - 1;
128       mNeedTrigger       = true;
129       mNeedDroppedFrames = false;
130     }
131
132     if(mDynamicPropertyCallback)
133     {
134       CallbackBase::ExecuteReturn<Property::Value>(*mDynamicPropertyCallback, 0, 0, frameNumber);
135     }
136
137     if(mNeedTrigger)
138     {
139       mEventThreadCallback->Trigger();
140       mNeedTrigger = false;
141     }
142
143     if(frameNumber == 1 && mPreviousFrame != frameNumber)
144     {
145       mPreviousFrame = frameNumber;
146       // For test corverage
147       return false;
148     }
149     mPreviousFrame = frameNumber;
150     return true;
151   }
152
153   uint32_t GetTotalFrameNumber() const
154   {
155     return mTotalFrameNumber;
156   }
157
158   float GetFrameRate() const
159   {
160     return mFrameRate;
161   }
162
163   void GetDefaultSize(uint32_t& width, uint32_t& height) const
164   {
165     width  = mDefaultWidth;
166     height = mDefaultHeight;
167   }
168
169   bool GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
170   {
171     if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_1) == 0)
172     {
173       startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_1;
174       endFrame   = VECTOR_ANIMATION_MARKER_END_FRAME_1;
175     }
176     else if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_2) == 0)
177     {
178       startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_2;
179       endFrame   = VECTOR_ANIMATION_MARKER_END_FRAME_2;
180     }
181     else
182     {
183       return false;
184     }
185     return true;
186   }
187
188   void InvalidateBuffer()
189   {
190     Dali::Mutex::ScopedLock lock(mMutex);
191     if(mResourceReady)
192     {
193       mNeedTrigger   = true;
194       mResourceReady = false;
195     }
196   }
197
198   void AddPropertyValueCallback(const std::string& keyPath, Dali::VectorAnimationRenderer::VectorProperty property, CallbackBase* callback, int32_t id)
199   {
200     mDynamicPropertyCallback = std::unique_ptr<CallbackBase>(callback);
201   }
202
203   Dali::VectorAnimationRenderer::UploadCompletedSignalType& UploadCompletedSignal()
204   {
205     return mUploadCompletedSignal;
206   }
207
208   void OnTriggered()
209   {
210     if(!mResourceReady)
211     {
212       mResourceReady = true;
213
214       Dali::TextureSet textureSet = mRenderer.GetTextures();
215       Dali::Texture    texture    = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight);
216       textureSet.SetTexture(0, texture);
217
218       Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(mWidth, mHeight, Pixel::RGBA8888);
219       Dali::PixelData    pixelData   = Devel::PixelBuffer::Convert(pixelBuffer);
220       texture.Upload(pixelData);
221
222       mUploadCompletedSignal.Emit();
223     }
224   }
225
226 public:
227   static uint32_t mCount;
228
229   std::string                   mUrl;
230   Dali::Renderer                mRenderer;
231   Dali::Mutex                   mMutex;
232   std::unique_ptr<CallbackBase> mDynamicPropertyCallback{nullptr};
233
234   uint32_t mWidth;
235   uint32_t mHeight;
236   uint32_t mDefaultWidth;
237   uint32_t mDefaultHeight;
238   uint32_t mTotalFrameNumber;
239   uint32_t mPreviousFrame;
240   uint32_t mDelayTime;
241   uint32_t mDroppedFrames;
242   float    mFrameRate;
243   bool     mTestFrameDrop;
244   bool     mNeedDroppedFrames;
245   bool     mLoadFailed{false};
246   bool     mResourceReady{false};
247   bool     mNeedTrigger{true};
248
249   Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
250   std::unique_ptr<EventThreadCallback>                     mEventThreadCallback;
251 };
252
253 uint32_t VectorAnimationRenderer::mCount = 0;
254
255 inline VectorAnimationRenderer& GetImplementation(Dali::VectorAnimationRenderer& renderer)
256 {
257   DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
258   BaseObject& handle = renderer.GetBaseObject();
259   return static_cast<Internal::Adaptor::VectorAnimationRenderer&>(handle);
260 }
261
262 inline const VectorAnimationRenderer& GetImplementation(const Dali::VectorAnimationRenderer& renderer)
263 {
264   DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
265   const BaseObject& handle = renderer.GetBaseObject();
266   return static_cast<const Internal::Adaptor::VectorAnimationRenderer&>(handle);
267 }
268
269 } // namespace Adaptor
270
271 } // namespace Internal
272
273 /********************************************************************************/
274 /*********************************  PUBLIC CLASS  *******************************/
275 /********************************************************************************/
276
277 VectorAnimationRenderer VectorAnimationRenderer::New()
278 {
279   Internal::Adaptor::VectorAnimationRenderer* animationRenderer = new Internal::Adaptor::VectorAnimationRenderer();
280
281   Internal::Adaptor::gVectorAnimationRenderer = animationRenderer;
282
283   return VectorAnimationRenderer(animationRenderer);
284 }
285
286 VectorAnimationRenderer::VectorAnimationRenderer()
287 {
288 }
289
290 VectorAnimationRenderer::~VectorAnimationRenderer()
291 {
292 }
293
294 VectorAnimationRenderer::VectorAnimationRenderer(Internal::Adaptor::VectorAnimationRenderer* internal)
295 : BaseHandle(internal)
296 {
297 }
298
299 VectorAnimationRenderer::VectorAnimationRenderer(const VectorAnimationRenderer& handle)
300 : BaseHandle(handle)
301 {
302 }
303
304 VectorAnimationRenderer& VectorAnimationRenderer::operator=(const VectorAnimationRenderer& rhs)
305 {
306   BaseHandle::operator=(rhs);
307   return *this;
308 }
309
310 void VectorAnimationRenderer::Finalize()
311 {
312 }
313
314 bool VectorAnimationRenderer::Load(const std::string& url)
315 {
316   return Internal::Adaptor::GetImplementation(*this).Load(url);
317 }
318
319 void VectorAnimationRenderer::SetRenderer(Renderer renderer)
320 {
321   Internal::Adaptor::GetImplementation(*this).SetRenderer(renderer);
322 }
323
324 void VectorAnimationRenderer::SetSize(uint32_t width, uint32_t height)
325 {
326   Internal::Adaptor::GetImplementation(*this).SetSize(width, height);
327 }
328
329 bool VectorAnimationRenderer::Render(uint32_t frameNumber)
330 {
331   return Internal::Adaptor::GetImplementation(*this).Render(frameNumber);
332 }
333
334 uint32_t VectorAnimationRenderer::GetTotalFrameNumber() const
335 {
336   return Internal::Adaptor::GetImplementation(*this).GetTotalFrameNumber();
337 }
338
339 float VectorAnimationRenderer::GetFrameRate() const
340 {
341   return Internal::Adaptor::GetImplementation(*this).GetFrameRate();
342 }
343
344 void VectorAnimationRenderer::GetDefaultSize(uint32_t& width, uint32_t& height) const
345 {
346   Internal::Adaptor::GetImplementation(*this).GetDefaultSize(width, height);
347 }
348
349 void VectorAnimationRenderer::GetLayerInfo(Property::Map& map) const
350 {
351 }
352
353 bool VectorAnimationRenderer::GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
354 {
355   return Internal::Adaptor::GetImplementation(*this).GetMarkerInfo(marker, startFrame, endFrame);
356 }
357
358 void VectorAnimationRenderer::InvalidateBuffer()
359 {
360   return Internal::Adaptor::GetImplementation(*this).InvalidateBuffer();
361 }
362
363 void VectorAnimationRenderer::AddPropertyValueCallback(const std::string& keyPath, VectorProperty property, CallbackBase* callback, int32_t id)
364 {
365   Internal::Adaptor::GetImplementation(*this).AddPropertyValueCallback(keyPath, property, callback, id);
366 }
367
368 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
369 {
370   return Internal::Adaptor::GetImplementation(*this).UploadCompletedSignal();
371 }
372
373 } // namespace Dali
374
375 namespace Test
376 {
377 namespace VectorAnimationRenderer
378 {
379 void DelayRendering(uint32_t delay)
380 {
381   Dali::Internal::Adaptor::gVectorAnimationRenderer->mDelayTime     = delay;
382   Dali::Internal::Adaptor::gVectorAnimationRenderer->mTestFrameDrop = true;
383 }
384
385 uint32_t GetDroppedFrames()
386 {
387   return Dali::Internal::Adaptor::gVectorAnimationRenderer->mDroppedFrames;
388 }
389
390 } // namespace VectorAnimationRenderer
391 } // namespace Test