2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 <dali/public-api/object/property-array.h>
23 #include <toolkit-application.h>
24 #include <toolkit-event-thread-callback.h>
25 #include <toolkit-vector-animation-renderer.h>
38 Dali::Internal::Adaptor::VectorAnimationRenderer* gVectorAnimationRenderer = nullptr;
41 class VectorAnimationRenderer : public Dali::BaseObject
44 VectorAnimationRenderer()
51 mTotalFrameNumber(VECTOR_ANIMATION_TOTAL_FRAME_NUMBER),
56 mTestFrameDrop(false),
57 mNeedDroppedFrames(false),
58 mEventThreadCallback(new EventThreadCallback(MakeCallback(this, &VectorAnimationRenderer::OnTriggered)))
68 ~VectorAnimationRenderer()
73 bool Load(const std::string& url)
75 Dali::Mutex::ScopedLock lock(mMutex);
77 if(mUrl == "invalid.json")
82 else if(mUrl == "framedrop.json")
84 // Change total frame number for test
85 mTotalFrameNumber = 200;
94 bool Load(const Dali::Vector<uint8_t>& data)
96 Dali::Mutex::ScopedLock lock(mMutex);
104 void SetRenderer(Dali::Renderer renderer)
106 mRenderer = renderer;
109 void SetSize(uint32_t width, uint32_t height)
111 Dali::Mutex::ScopedLock lock(mMutex);
118 mResourceReady = false;
122 bool Render(uint32_t frameNumber)
124 Dali::Mutex::ScopedLock lock(mMutex);
125 if(mWidth == 0 || mHeight == 0)
132 std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int32_t>(mDelayTime)));
133 mTestFrameDrop = false;
134 mNeedDroppedFrames = true;
136 else if(mNeedDroppedFrames)
138 mDroppedFrames = (frameNumber > mPreviousFrame) ? frameNumber - mPreviousFrame - 1 : frameNumber + (mTotalFrameNumber - mPreviousFrame) - 1;
140 mNeedDroppedFrames = false;
143 if(mDynamicPropertyCallback)
145 CallbackBase::ExecuteReturn<Property::Value>(*mDynamicPropertyCallback, 0, 0, frameNumber);
150 mEventThreadCallback->Trigger();
151 mNeedTrigger = false;
154 if(frameNumber == 1 && mPreviousFrame != frameNumber)
156 mPreviousFrame = frameNumber;
157 // For test corverage
160 mPreviousFrame = frameNumber;
164 uint32_t GetTotalFrameNumber() const
166 return mTotalFrameNumber;
169 float GetFrameRate() const
174 void GetDefaultSize(uint32_t& width, uint32_t& height) const
176 width = mDefaultWidth;
177 height = mDefaultHeight;
180 bool GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
182 if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_1) == 0)
184 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_1;
185 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_1;
187 else if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_2) == 0)
189 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_2;
190 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_2;
199 void InvalidateBuffer()
201 Dali::Mutex::ScopedLock lock(mMutex);
205 mResourceReady = false;
209 void AddPropertyValueCallback(const std::string& keyPath, Dali::VectorAnimationRenderer::VectorProperty property, CallbackBase* callback, int32_t id)
211 mDynamicPropertyCallback = std::unique_ptr<CallbackBase>(callback);
214 Dali::VectorAnimationRenderer::UploadCompletedSignalType& UploadCompletedSignal()
216 return mUploadCompletedSignal;
223 mResourceReady = true;
225 Dali::TextureSet textureSet = mRenderer.GetTextures();
226 Dali::Texture texture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight);
227 textureSet.SetTexture(0, texture);
229 Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(mWidth, mHeight, Pixel::RGBA8888);
230 Dali::PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
231 texture.Upload(pixelData);
233 mUploadCompletedSignal.Emit();
238 static uint32_t mCount;
241 Dali::Renderer mRenderer;
243 std::unique_ptr<CallbackBase> mDynamicPropertyCallback{nullptr};
247 uint32_t mDefaultWidth;
248 uint32_t mDefaultHeight;
249 uint32_t mTotalFrameNumber;
250 uint32_t mPreviousFrame;
252 uint32_t mDroppedFrames;
255 bool mNeedDroppedFrames;
256 bool mLoadFailed{false};
257 bool mResourceReady{false};
258 bool mNeedTrigger{true};
260 Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
261 std::unique_ptr<EventThreadCallback> mEventThreadCallback;
264 uint32_t VectorAnimationRenderer::mCount = 0;
266 inline VectorAnimationRenderer& GetImplementation(Dali::VectorAnimationRenderer& renderer)
268 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
269 BaseObject& handle = renderer.GetBaseObject();
270 return static_cast<Internal::Adaptor::VectorAnimationRenderer&>(handle);
273 inline const VectorAnimationRenderer& GetImplementation(const Dali::VectorAnimationRenderer& renderer)
275 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
276 const BaseObject& handle = renderer.GetBaseObject();
277 return static_cast<const Internal::Adaptor::VectorAnimationRenderer&>(handle);
280 } // namespace Adaptor
282 } // namespace Internal
284 /********************************************************************************/
285 /********************************* PUBLIC CLASS *******************************/
286 /********************************************************************************/
288 VectorAnimationRenderer VectorAnimationRenderer::New()
290 Internal::Adaptor::VectorAnimationRenderer* animationRenderer = new Internal::Adaptor::VectorAnimationRenderer();
292 Internal::Adaptor::gVectorAnimationRenderer = animationRenderer;
294 return VectorAnimationRenderer(animationRenderer);
297 VectorAnimationRenderer::VectorAnimationRenderer()
301 VectorAnimationRenderer::~VectorAnimationRenderer()
305 VectorAnimationRenderer::VectorAnimationRenderer(Internal::Adaptor::VectorAnimationRenderer* internal)
306 : BaseHandle(internal)
310 VectorAnimationRenderer::VectorAnimationRenderer(const VectorAnimationRenderer& handle)
315 VectorAnimationRenderer& VectorAnimationRenderer::operator=(const VectorAnimationRenderer& rhs)
317 BaseHandle::operator=(rhs);
321 void VectorAnimationRenderer::Finalize()
325 bool VectorAnimationRenderer::Load(const std::string& url)
327 return Internal::Adaptor::GetImplementation(*this).Load(url);
330 bool VectorAnimationRenderer::Load(const Dali::Vector<uint8_t>& data)
332 return Internal::Adaptor::GetImplementation(*this).Load(data);
335 void VectorAnimationRenderer::SetRenderer(Renderer renderer)
337 Internal::Adaptor::GetImplementation(*this).SetRenderer(renderer);
340 void VectorAnimationRenderer::SetSize(uint32_t width, uint32_t height)
342 Internal::Adaptor::GetImplementation(*this).SetSize(width, height);
345 bool VectorAnimationRenderer::Render(uint32_t frameNumber)
347 return Internal::Adaptor::GetImplementation(*this).Render(frameNumber);
350 void VectorAnimationRenderer::RenderStopped()
354 uint32_t VectorAnimationRenderer::GetTotalFrameNumber() const
356 return Internal::Adaptor::GetImplementation(*this).GetTotalFrameNumber();
359 float VectorAnimationRenderer::GetFrameRate() const
361 return Internal::Adaptor::GetImplementation(*this).GetFrameRate();
364 void VectorAnimationRenderer::GetDefaultSize(uint32_t& width, uint32_t& height) const
366 Internal::Adaptor::GetImplementation(*this).GetDefaultSize(width, height);
369 void VectorAnimationRenderer::GetLayerInfo(Property::Map& map) const
373 bool VectorAnimationRenderer::GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
375 return Internal::Adaptor::GetImplementation(*this).GetMarkerInfo(marker, startFrame, endFrame);
378 void VectorAnimationRenderer::GetMarkerInfo(Property::Map& map) const
380 map.Add(VECTOR_ANIMATION_MARKER_NAME_1, Property::Array({VECTOR_ANIMATION_MARKER_START_FRAME_1, VECTOR_ANIMATION_MARKER_END_FRAME_1}));
381 map.Add(VECTOR_ANIMATION_MARKER_NAME_2, Property::Array({VECTOR_ANIMATION_MARKER_START_FRAME_2, VECTOR_ANIMATION_MARKER_END_FRAME_2}));
384 void VectorAnimationRenderer::InvalidateBuffer()
386 return Internal::Adaptor::GetImplementation(*this).InvalidateBuffer();
389 void VectorAnimationRenderer::AddPropertyValueCallback(const std::string& keyPath, VectorProperty property, CallbackBase* callback, int32_t id)
391 Internal::Adaptor::GetImplementation(*this).AddPropertyValueCallback(keyPath, property, callback, id);
394 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
396 return Internal::Adaptor::GetImplementation(*this).UploadCompletedSignal();
403 namespace VectorAnimationRenderer
405 void DelayRendering(uint32_t delay)
407 Dali::Internal::Adaptor::gVectorAnimationRenderer->mDelayTime = delay;
408 Dali::Internal::Adaptor::gVectorAnimationRenderer->mTestFrameDrop = true;
411 uint32_t GetDroppedFrames()
413 return Dali::Internal::Adaptor::gVectorAnimationRenderer->mDroppedFrames;
416 } // namespace VectorAnimationRenderer