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 void KeepRasterizedBuffer()
216 Dali::Mutex::ScopedLock lock(mMutex);
217 mEnableFixedCache = true;
220 Dali::VectorAnimationRenderer::UploadCompletedSignalType& UploadCompletedSignal()
222 return mUploadCompletedSignal;
229 mResourceReady = true;
231 Dali::TextureSet textureSet = mRenderer.GetTextures();
232 Dali::Texture texture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight);
233 textureSet.SetTexture(0, texture);
235 Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(mWidth, mHeight, Pixel::RGBA8888);
236 Dali::PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
237 texture.Upload(pixelData);
239 mUploadCompletedSignal.Emit();
244 static uint32_t mCount;
247 Dali::Renderer mRenderer;
249 std::unique_ptr<CallbackBase> mDynamicPropertyCallback{nullptr};
253 uint32_t mDefaultWidth;
254 uint32_t mDefaultHeight;
255 uint32_t mTotalFrameNumber;
256 uint32_t mPreviousFrame;
258 uint32_t mDroppedFrames;
261 bool mNeedDroppedFrames;
262 bool mLoadFailed{false};
263 bool mResourceReady{false};
264 bool mNeedTrigger{true};
265 bool mEnableFixedCache{false};
267 Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
268 std::unique_ptr<EventThreadCallback> mEventThreadCallback;
271 uint32_t VectorAnimationRenderer::mCount = 0;
273 inline VectorAnimationRenderer& GetImplementation(Dali::VectorAnimationRenderer& renderer)
275 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
276 BaseObject& handle = renderer.GetBaseObject();
277 return static_cast<Internal::Adaptor::VectorAnimationRenderer&>(handle);
280 inline const VectorAnimationRenderer& GetImplementation(const Dali::VectorAnimationRenderer& renderer)
282 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
283 const BaseObject& handle = renderer.GetBaseObject();
284 return static_cast<const Internal::Adaptor::VectorAnimationRenderer&>(handle);
287 } // namespace Adaptor
289 } // namespace Internal
291 /********************************************************************************/
292 /********************************* PUBLIC CLASS *******************************/
293 /********************************************************************************/
295 VectorAnimationRenderer VectorAnimationRenderer::New()
297 Internal::Adaptor::VectorAnimationRenderer* animationRenderer = new Internal::Adaptor::VectorAnimationRenderer();
299 Internal::Adaptor::gVectorAnimationRenderer = animationRenderer;
301 return VectorAnimationRenderer(animationRenderer);
304 VectorAnimationRenderer::VectorAnimationRenderer()
308 VectorAnimationRenderer::~VectorAnimationRenderer()
312 VectorAnimationRenderer::VectorAnimationRenderer(Internal::Adaptor::VectorAnimationRenderer* internal)
313 : BaseHandle(internal)
317 VectorAnimationRenderer::VectorAnimationRenderer(const VectorAnimationRenderer& handle)
322 VectorAnimationRenderer& VectorAnimationRenderer::operator=(const VectorAnimationRenderer& rhs)
324 BaseHandle::operator=(rhs);
328 void VectorAnimationRenderer::Finalize()
332 bool VectorAnimationRenderer::Load(const std::string& url)
334 return Internal::Adaptor::GetImplementation(*this).Load(url);
337 bool VectorAnimationRenderer::Load(const Dali::Vector<uint8_t>& data)
339 return Internal::Adaptor::GetImplementation(*this).Load(data);
342 void VectorAnimationRenderer::SetRenderer(Renderer renderer)
344 Internal::Adaptor::GetImplementation(*this).SetRenderer(renderer);
347 void VectorAnimationRenderer::SetSize(uint32_t width, uint32_t height)
349 Internal::Adaptor::GetImplementation(*this).SetSize(width, height);
352 bool VectorAnimationRenderer::Render(uint32_t frameNumber)
354 return Internal::Adaptor::GetImplementation(*this).Render(frameNumber);
357 void VectorAnimationRenderer::RenderStopped()
361 uint32_t VectorAnimationRenderer::GetTotalFrameNumber() const
363 return Internal::Adaptor::GetImplementation(*this).GetTotalFrameNumber();
366 float VectorAnimationRenderer::GetFrameRate() const
368 return Internal::Adaptor::GetImplementation(*this).GetFrameRate();
371 void VectorAnimationRenderer::GetDefaultSize(uint32_t& width, uint32_t& height) const
373 Internal::Adaptor::GetImplementation(*this).GetDefaultSize(width, height);
376 void VectorAnimationRenderer::GetLayerInfo(Property::Map& map) const
380 bool VectorAnimationRenderer::GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
382 return Internal::Adaptor::GetImplementation(*this).GetMarkerInfo(marker, startFrame, endFrame);
385 void VectorAnimationRenderer::GetMarkerInfo(Property::Map& map) const
387 map.Add(VECTOR_ANIMATION_MARKER_NAME_1, Property::Array({VECTOR_ANIMATION_MARKER_START_FRAME_1, VECTOR_ANIMATION_MARKER_END_FRAME_1}));
388 map.Add(VECTOR_ANIMATION_MARKER_NAME_2, Property::Array({VECTOR_ANIMATION_MARKER_START_FRAME_2, VECTOR_ANIMATION_MARKER_END_FRAME_2}));
391 void VectorAnimationRenderer::InvalidateBuffer()
393 return Internal::Adaptor::GetImplementation(*this).InvalidateBuffer();
396 void VectorAnimationRenderer::AddPropertyValueCallback(const std::string& keyPath, VectorProperty property, CallbackBase* callback, int32_t id)
398 Internal::Adaptor::GetImplementation(*this).AddPropertyValueCallback(keyPath, property, callback, id);
401 void VectorAnimationRenderer::KeepRasterizedBuffer()
403 Internal::Adaptor::GetImplementation(*this).KeepRasterizedBuffer();
407 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
409 return Internal::Adaptor::GetImplementation(*this).UploadCompletedSignal();
416 namespace VectorAnimationRenderer
418 void DelayRendering(uint32_t delay)
420 Dali::Internal::Adaptor::gVectorAnimationRenderer->mDelayTime = delay;
421 Dali::Internal::Adaptor::gVectorAnimationRenderer->mTestFrameDrop = true;
424 uint32_t GetDroppedFrames()
426 return Dali::Internal::Adaptor::gVectorAnimationRenderer->mDroppedFrames;
429 } // namespace VectorAnimationRenderer