2 * Copyright (c) 2022 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 <toolkit-application.h>
23 #include <toolkit-event-thread-callback.h>
24 #include <toolkit-vector-animation-renderer.h>
37 Dali::Internal::Adaptor::VectorAnimationRenderer* gVectorAnimationRenderer = nullptr;
40 class VectorAnimationRenderer : public Dali::BaseObject
43 VectorAnimationRenderer()
50 mTotalFrameNumber(VECTOR_ANIMATION_TOTAL_FRAME_NUMBER),
55 mTestFrameDrop(false),
56 mNeedDroppedFrames(false),
57 mEventThreadCallback(new EventThreadCallback(MakeCallback(this, &VectorAnimationRenderer::OnTriggered)))
67 ~VectorAnimationRenderer()
72 bool Load(const std::string& url)
74 Dali::Mutex::ScopedLock lock(mMutex);
76 if(mUrl == "invalid.json")
81 else if(mUrl == "framedrop.json")
83 // Change total frame number for test
84 mTotalFrameNumber = 200;
93 bool Load(const Dali::Vector<uint8_t>& data)
95 Dali::Mutex::ScopedLock lock(mMutex);
103 void SetRenderer(Dali::Renderer renderer)
105 mRenderer = renderer;
108 void SetSize(uint32_t width, uint32_t height)
110 Dali::Mutex::ScopedLock lock(mMutex);
117 mResourceReady = false;
121 bool Render(uint32_t frameNumber)
123 Dali::Mutex::ScopedLock lock(mMutex);
124 if(mWidth == 0 || mHeight == 0)
131 std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int32_t>(mDelayTime)));
132 mTestFrameDrop = false;
133 mNeedDroppedFrames = true;
135 else if(mNeedDroppedFrames)
137 mDroppedFrames = (frameNumber > mPreviousFrame) ? frameNumber - mPreviousFrame - 1 : frameNumber + (mTotalFrameNumber - mPreviousFrame) - 1;
139 mNeedDroppedFrames = false;
142 if(mDynamicPropertyCallback)
144 CallbackBase::ExecuteReturn<Property::Value>(*mDynamicPropertyCallback, 0, 0, frameNumber);
149 mEventThreadCallback->Trigger();
150 mNeedTrigger = false;
153 if(frameNumber == 1 && mPreviousFrame != frameNumber)
155 mPreviousFrame = frameNumber;
156 // For test corverage
159 mPreviousFrame = frameNumber;
163 uint32_t GetTotalFrameNumber() const
165 return mTotalFrameNumber;
168 float GetFrameRate() const
173 void GetDefaultSize(uint32_t& width, uint32_t& height) const
175 width = mDefaultWidth;
176 height = mDefaultHeight;
179 bool GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
181 if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_1) == 0)
183 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_1;
184 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_1;
186 else if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_2) == 0)
188 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_2;
189 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_2;
198 void InvalidateBuffer()
200 Dali::Mutex::ScopedLock lock(mMutex);
204 mResourceReady = false;
208 void AddPropertyValueCallback(const std::string& keyPath, Dali::VectorAnimationRenderer::VectorProperty property, CallbackBase* callback, int32_t id)
210 mDynamicPropertyCallback = std::unique_ptr<CallbackBase>(callback);
213 Dali::VectorAnimationRenderer::UploadCompletedSignalType& UploadCompletedSignal()
215 return mUploadCompletedSignal;
222 mResourceReady = true;
224 Dali::TextureSet textureSet = mRenderer.GetTextures();
225 Dali::Texture texture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight);
226 textureSet.SetTexture(0, texture);
228 Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(mWidth, mHeight, Pixel::RGBA8888);
229 Dali::PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
230 texture.Upload(pixelData);
232 mUploadCompletedSignal.Emit();
237 static uint32_t mCount;
240 Dali::Renderer mRenderer;
242 std::unique_ptr<CallbackBase> mDynamicPropertyCallback{nullptr};
246 uint32_t mDefaultWidth;
247 uint32_t mDefaultHeight;
248 uint32_t mTotalFrameNumber;
249 uint32_t mPreviousFrame;
251 uint32_t mDroppedFrames;
254 bool mNeedDroppedFrames;
255 bool mLoadFailed{false};
256 bool mResourceReady{false};
257 bool mNeedTrigger{true};
259 Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
260 std::unique_ptr<EventThreadCallback> mEventThreadCallback;
263 uint32_t VectorAnimationRenderer::mCount = 0;
265 inline VectorAnimationRenderer& GetImplementation(Dali::VectorAnimationRenderer& renderer)
267 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
268 BaseObject& handle = renderer.GetBaseObject();
269 return static_cast<Internal::Adaptor::VectorAnimationRenderer&>(handle);
272 inline const VectorAnimationRenderer& GetImplementation(const Dali::VectorAnimationRenderer& renderer)
274 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
275 const BaseObject& handle = renderer.GetBaseObject();
276 return static_cast<const Internal::Adaptor::VectorAnimationRenderer&>(handle);
279 } // namespace Adaptor
281 } // namespace Internal
283 /********************************************************************************/
284 /********************************* PUBLIC CLASS *******************************/
285 /********************************************************************************/
287 VectorAnimationRenderer VectorAnimationRenderer::New()
289 Internal::Adaptor::VectorAnimationRenderer* animationRenderer = new Internal::Adaptor::VectorAnimationRenderer();
291 Internal::Adaptor::gVectorAnimationRenderer = animationRenderer;
293 return VectorAnimationRenderer(animationRenderer);
296 VectorAnimationRenderer::VectorAnimationRenderer()
300 VectorAnimationRenderer::~VectorAnimationRenderer()
304 VectorAnimationRenderer::VectorAnimationRenderer(Internal::Adaptor::VectorAnimationRenderer* internal)
305 : BaseHandle(internal)
309 VectorAnimationRenderer::VectorAnimationRenderer(const VectorAnimationRenderer& handle)
314 VectorAnimationRenderer& VectorAnimationRenderer::operator=(const VectorAnimationRenderer& rhs)
316 BaseHandle::operator=(rhs);
320 void VectorAnimationRenderer::Finalize()
324 bool VectorAnimationRenderer::Load(const std::string& url)
326 return Internal::Adaptor::GetImplementation(*this).Load(url);
329 bool VectorAnimationRenderer::Load(const Dali::Vector<uint8_t>& data)
331 return Internal::Adaptor::GetImplementation(*this).Load(data);
334 void VectorAnimationRenderer::SetRenderer(Renderer renderer)
336 Internal::Adaptor::GetImplementation(*this).SetRenderer(renderer);
339 void VectorAnimationRenderer::SetSize(uint32_t width, uint32_t height)
341 Internal::Adaptor::GetImplementation(*this).SetSize(width, height);
344 bool VectorAnimationRenderer::Render(uint32_t frameNumber)
346 return Internal::Adaptor::GetImplementation(*this).Render(frameNumber);
349 void VectorAnimationRenderer::RenderStopped()
353 uint32_t VectorAnimationRenderer::GetTotalFrameNumber() const
355 return Internal::Adaptor::GetImplementation(*this).GetTotalFrameNumber();
358 float VectorAnimationRenderer::GetFrameRate() const
360 return Internal::Adaptor::GetImplementation(*this).GetFrameRate();
363 void VectorAnimationRenderer::GetDefaultSize(uint32_t& width, uint32_t& height) const
365 Internal::Adaptor::GetImplementation(*this).GetDefaultSize(width, height);
368 void VectorAnimationRenderer::GetLayerInfo(Property::Map& map) const
372 bool VectorAnimationRenderer::GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
374 return Internal::Adaptor::GetImplementation(*this).GetMarkerInfo(marker, startFrame, endFrame);
377 void VectorAnimationRenderer::InvalidateBuffer()
379 return Internal::Adaptor::GetImplementation(*this).InvalidateBuffer();
382 void VectorAnimationRenderer::AddPropertyValueCallback(const std::string& keyPath, VectorProperty property, CallbackBase* callback, int32_t id)
384 Internal::Adaptor::GetImplementation(*this).AddPropertyValueCallback(keyPath, property, callback, id);
387 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
389 return Internal::Adaptor::GetImplementation(*this).UploadCompletedSignal();
396 namespace VectorAnimationRenderer
398 void DelayRendering(uint32_t delay)
400 Dali::Internal::Adaptor::gVectorAnimationRenderer->mDelayTime = delay;
401 Dali::Internal::Adaptor::gVectorAnimationRenderer->mTestFrameDrop = true;
404 uint32_t GetDroppedFrames()
406 return Dali::Internal::Adaptor::gVectorAnimationRenderer->mDroppedFrames;
409 } // namespace VectorAnimationRenderer