2 * Copyright (c) 2024 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/adaptor-framework/native-image-source.h>
22 #include <dali/public-api/object/base-object.h>
23 #include <dali/public-api/object/property-array.h>
24 #include <toolkit-application.h>
25 #include <toolkit-event-thread-callback.h>
26 #include <toolkit-vector-animation-renderer.h>
39 Dali::Internal::Adaptor::VectorAnimationRenderer* gVectorAnimationRenderer = nullptr;
42 class VectorAnimationRenderer : public Dali::BaseObject
45 VectorAnimationRenderer()
52 mTotalFrameNumber(VECTOR_ANIMATION_TOTAL_FRAME_NUMBER),
57 mTestFrameDrop(false),
58 mNeedDroppedFrames(false),
59 mUseNativeImage(false),
60 mEventThreadCallback(new EventThreadCallback(MakeCallback(this, &VectorAnimationRenderer::OnTriggered)))
70 ~VectorAnimationRenderer()
75 bool Load(const std::string& url)
77 Dali::Mutex::ScopedLock lock(mMutex);
79 if(mUrl == "invalid.json")
84 else if(mUrl == "framedrop.json")
86 // Change total frame number for test
87 mTotalFrameNumber = 200;
96 bool Load(const Dali::Vector<uint8_t>& data)
98 Dali::Mutex::ScopedLock lock(mMutex);
101 mDefaultHeight = 100;
106 void SetRenderer(Dali::Renderer renderer)
108 mRenderer = renderer;
111 void SetSize(uint32_t width, uint32_t height)
113 Dali::Mutex::ScopedLock lock(mMutex);
120 mResourceReady = false;
124 bool Render(uint32_t frameNumber)
126 Dali::Mutex::ScopedLock lock(mMutex);
127 if(mWidth == 0 || mHeight == 0)
134 std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int32_t>(mDelayTime)));
135 mTestFrameDrop = false;
136 mNeedDroppedFrames = true;
138 else if(mNeedDroppedFrames)
140 mDroppedFrames = (frameNumber > mPreviousFrame) ? frameNumber - mPreviousFrame - 1 : frameNumber + (mTotalFrameNumber - mPreviousFrame) - 1;
142 mNeedDroppedFrames = false;
145 if(mDynamicPropertyCallback)
147 CallbackBase::ExecuteReturn<Property::Value>(*mDynamicPropertyCallback, 0, 0, frameNumber);
152 mEventThreadCallback->Trigger();
153 mNeedTrigger = false;
156 if(frameNumber == 1 && mPreviousFrame != frameNumber)
158 mPreviousFrame = frameNumber;
159 // For test corverage
162 mPreviousFrame = frameNumber;
166 uint32_t GetTotalFrameNumber() const
168 return mTotalFrameNumber;
171 float GetFrameRate() const
176 void GetDefaultSize(uint32_t& width, uint32_t& height) const
178 width = mDefaultWidth;
179 height = mDefaultHeight;
182 bool GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
184 if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_1) == 0)
186 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_1;
187 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_1;
189 else if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_2) == 0)
191 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_2;
192 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_2;
201 void InvalidateBuffer()
203 Dali::Mutex::ScopedLock lock(mMutex);
207 mResourceReady = false;
211 void AddPropertyValueCallback(const std::string& keyPath, Dali::VectorAnimationRenderer::VectorProperty property, CallbackBase* callback, int32_t id)
213 mDynamicPropertyCallback = std::unique_ptr<CallbackBase>(callback);
216 void KeepRasterizedBuffer()
218 Dali::Mutex::ScopedLock lock(mMutex);
219 mEnableFixedCache = true;
222 Dali::VectorAnimationRenderer::UploadCompletedSignalType& UploadCompletedSignal()
224 return mUploadCompletedSignal;
231 mResourceReady = true;
233 Dali::TextureSet textureSet = mRenderer.GetTextures();
237 Dali::NativeImageSourcePtr nativeImageSource = Dali::NativeImageSource::New(mWidth, mHeight, Dali::NativeImageSource::COLOR_DEPTH_32);
238 Dali::Texture texture = Dali::Texture::New(*nativeImageSource);
239 textureSet.SetTexture(0, texture);
243 Dali::Texture texture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight);
244 textureSet.SetTexture(0, texture);
246 Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(mWidth, mHeight, Pixel::RGBA8888);
247 Dali::PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
248 texture.Upload(pixelData);
251 mUploadCompletedSignal.Emit();
256 static uint32_t mCount;
259 Dali::Renderer mRenderer;
261 std::unique_ptr<CallbackBase> mDynamicPropertyCallback{nullptr};
265 uint32_t mDefaultWidth;
266 uint32_t mDefaultHeight;
267 uint32_t mTotalFrameNumber;
268 uint32_t mPreviousFrame;
270 uint32_t mDroppedFrames;
273 bool mNeedDroppedFrames;
274 bool mLoadFailed{false};
275 bool mResourceReady{false};
276 bool mNeedTrigger{true};
277 bool mEnableFixedCache{false};
278 bool mUseNativeImage{false};
280 Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
281 std::unique_ptr<EventThreadCallback> mEventThreadCallback;
284 uint32_t VectorAnimationRenderer::mCount = 0;
286 inline VectorAnimationRenderer& GetImplementation(Dali::VectorAnimationRenderer& renderer)
288 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
289 BaseObject& handle = renderer.GetBaseObject();
290 return static_cast<Internal::Adaptor::VectorAnimationRenderer&>(handle);
293 inline const VectorAnimationRenderer& GetImplementation(const Dali::VectorAnimationRenderer& renderer)
295 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
296 const BaseObject& handle = renderer.GetBaseObject();
297 return static_cast<const Internal::Adaptor::VectorAnimationRenderer&>(handle);
300 } // namespace Adaptor
302 } // namespace Internal
304 /********************************************************************************/
305 /********************************* PUBLIC CLASS *******************************/
306 /********************************************************************************/
308 VectorAnimationRenderer VectorAnimationRenderer::New()
310 Internal::Adaptor::VectorAnimationRenderer* animationRenderer = new Internal::Adaptor::VectorAnimationRenderer();
312 Internal::Adaptor::gVectorAnimationRenderer = animationRenderer;
314 return VectorAnimationRenderer(animationRenderer);
317 VectorAnimationRenderer::VectorAnimationRenderer()
321 VectorAnimationRenderer::~VectorAnimationRenderer()
325 VectorAnimationRenderer::VectorAnimationRenderer(Internal::Adaptor::VectorAnimationRenderer* internal)
326 : BaseHandle(internal)
330 VectorAnimationRenderer::VectorAnimationRenderer(const VectorAnimationRenderer& handle)
335 VectorAnimationRenderer& VectorAnimationRenderer::operator=(const VectorAnimationRenderer& rhs)
337 BaseHandle::operator=(rhs);
341 void VectorAnimationRenderer::Finalize()
345 bool VectorAnimationRenderer::Load(const std::string& url)
347 return Internal::Adaptor::GetImplementation(*this).Load(url);
350 bool VectorAnimationRenderer::Load(const Dali::Vector<uint8_t>& data)
352 return Internal::Adaptor::GetImplementation(*this).Load(data);
355 void VectorAnimationRenderer::SetRenderer(Renderer renderer)
357 Internal::Adaptor::GetImplementation(*this).SetRenderer(renderer);
360 void VectorAnimationRenderer::SetSize(uint32_t width, uint32_t height)
362 Internal::Adaptor::GetImplementation(*this).SetSize(width, height);
365 bool VectorAnimationRenderer::Render(uint32_t frameNumber)
367 return Internal::Adaptor::GetImplementation(*this).Render(frameNumber);
370 void VectorAnimationRenderer::RenderStopped()
374 uint32_t VectorAnimationRenderer::GetTotalFrameNumber() const
376 return Internal::Adaptor::GetImplementation(*this).GetTotalFrameNumber();
379 float VectorAnimationRenderer::GetFrameRate() const
381 return Internal::Adaptor::GetImplementation(*this).GetFrameRate();
384 void VectorAnimationRenderer::GetDefaultSize(uint32_t& width, uint32_t& height) const
386 Internal::Adaptor::GetImplementation(*this).GetDefaultSize(width, height);
389 void VectorAnimationRenderer::GetLayerInfo(Property::Map& map) const
393 bool VectorAnimationRenderer::GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
395 return Internal::Adaptor::GetImplementation(*this).GetMarkerInfo(marker, startFrame, endFrame);
398 void VectorAnimationRenderer::GetMarkerInfo(Property::Map& map) const
400 map.Add(VECTOR_ANIMATION_MARKER_NAME_1, Property::Array({VECTOR_ANIMATION_MARKER_START_FRAME_1, VECTOR_ANIMATION_MARKER_END_FRAME_1}));
401 map.Add(VECTOR_ANIMATION_MARKER_NAME_2, Property::Array({VECTOR_ANIMATION_MARKER_START_FRAME_2, VECTOR_ANIMATION_MARKER_END_FRAME_2}));
404 void VectorAnimationRenderer::InvalidateBuffer()
406 return Internal::Adaptor::GetImplementation(*this).InvalidateBuffer();
409 void VectorAnimationRenderer::AddPropertyValueCallback(const std::string& keyPath, VectorProperty property, CallbackBase* callback, int32_t id)
411 Internal::Adaptor::GetImplementation(*this).AddPropertyValueCallback(keyPath, property, callback, id);
414 void VectorAnimationRenderer::KeepRasterizedBuffer()
416 Internal::Adaptor::GetImplementation(*this).KeepRasterizedBuffer();
419 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
421 return Internal::Adaptor::GetImplementation(*this).UploadCompletedSignal();
428 namespace VectorAnimationRenderer
430 void DelayRendering(uint32_t delay)
432 Dali::Internal::Adaptor::gVectorAnimationRenderer->mDelayTime = delay;
433 Dali::Internal::Adaptor::gVectorAnimationRenderer->mTestFrameDrop = true;
436 uint32_t GetDroppedFrames()
438 return Dali::Internal::Adaptor::gVectorAnimationRenderer->mDroppedFrames;
441 void UseNativeImageTexture(bool useNativeImage)
443 Dali::Internal::Adaptor::gVectorAnimationRenderer->mUseNativeImage = useNativeImage;
446 } // namespace VectorAnimationRenderer