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 void SetRenderer(Dali::Renderer renderer)
98 void SetSize(uint32_t width, uint32_t height)
100 Dali::Mutex::ScopedLock lock(mMutex);
107 mResourceReady = false;
111 bool Render(uint32_t frameNumber)
113 Dali::Mutex::ScopedLock lock(mMutex);
114 if(mWidth == 0 || mHeight == 0)
121 std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int32_t>(mDelayTime)));
122 mTestFrameDrop = false;
123 mNeedDroppedFrames = true;
125 else if(mNeedDroppedFrames)
127 mDroppedFrames = (frameNumber > mPreviousFrame) ? frameNumber - mPreviousFrame - 1 : frameNumber + (mTotalFrameNumber - mPreviousFrame) - 1;
129 mNeedDroppedFrames = false;
132 if(mDynamicPropertyCallback)
134 CallbackBase::ExecuteReturn<Property::Value>(*mDynamicPropertyCallback, 0, 0, frameNumber);
139 mEventThreadCallback->Trigger();
140 mNeedTrigger = false;
143 if(frameNumber == 1 && mPreviousFrame != frameNumber)
145 mPreviousFrame = frameNumber;
146 // For test corverage
149 mPreviousFrame = frameNumber;
153 uint32_t GetTotalFrameNumber() const
155 return mTotalFrameNumber;
158 float GetFrameRate() const
163 void GetDefaultSize(uint32_t& width, uint32_t& height) const
165 width = mDefaultWidth;
166 height = mDefaultHeight;
169 bool GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
171 if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_1) == 0)
173 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_1;
174 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_1;
176 else if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_2) == 0)
178 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_2;
179 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_2;
188 void InvalidateBuffer()
190 Dali::Mutex::ScopedLock lock(mMutex);
194 mResourceReady = false;
198 void AddPropertyValueCallback(const std::string& keyPath, Dali::VectorAnimationRenderer::VectorProperty property, CallbackBase* callback, int32_t id)
200 mDynamicPropertyCallback = std::unique_ptr<CallbackBase>(callback);
203 Dali::VectorAnimationRenderer::UploadCompletedSignalType& UploadCompletedSignal()
205 return mUploadCompletedSignal;
212 mResourceReady = true;
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);
218 Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(mWidth, mHeight, Pixel::RGBA8888);
219 Dali::PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
220 texture.Upload(pixelData);
222 mUploadCompletedSignal.Emit();
227 static uint32_t mCount;
230 Dali::Renderer mRenderer;
232 std::unique_ptr<CallbackBase> mDynamicPropertyCallback{nullptr};
236 uint32_t mDefaultWidth;
237 uint32_t mDefaultHeight;
238 uint32_t mTotalFrameNumber;
239 uint32_t mPreviousFrame;
241 uint32_t mDroppedFrames;
244 bool mNeedDroppedFrames;
245 bool mLoadFailed{false};
246 bool mResourceReady{false};
247 bool mNeedTrigger{true};
249 Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
250 std::unique_ptr<EventThreadCallback> mEventThreadCallback;
253 uint32_t VectorAnimationRenderer::mCount = 0;
255 inline VectorAnimationRenderer& GetImplementation(Dali::VectorAnimationRenderer& renderer)
257 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
258 BaseObject& handle = renderer.GetBaseObject();
259 return static_cast<Internal::Adaptor::VectorAnimationRenderer&>(handle);
262 inline const VectorAnimationRenderer& GetImplementation(const Dali::VectorAnimationRenderer& renderer)
264 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
265 const BaseObject& handle = renderer.GetBaseObject();
266 return static_cast<const Internal::Adaptor::VectorAnimationRenderer&>(handle);
269 } // namespace Adaptor
271 } // namespace Internal
273 /********************************************************************************/
274 /********************************* PUBLIC CLASS *******************************/
275 /********************************************************************************/
277 VectorAnimationRenderer VectorAnimationRenderer::New()
279 Internal::Adaptor::VectorAnimationRenderer* animationRenderer = new Internal::Adaptor::VectorAnimationRenderer();
281 Internal::Adaptor::gVectorAnimationRenderer = animationRenderer;
283 return VectorAnimationRenderer(animationRenderer);
286 VectorAnimationRenderer::VectorAnimationRenderer()
290 VectorAnimationRenderer::~VectorAnimationRenderer()
294 VectorAnimationRenderer::VectorAnimationRenderer(Internal::Adaptor::VectorAnimationRenderer* internal)
295 : BaseHandle(internal)
299 VectorAnimationRenderer::VectorAnimationRenderer(const VectorAnimationRenderer& handle)
304 VectorAnimationRenderer& VectorAnimationRenderer::operator=(const VectorAnimationRenderer& rhs)
306 BaseHandle::operator=(rhs);
310 void VectorAnimationRenderer::Finalize()
314 bool VectorAnimationRenderer::Load(const std::string& url)
316 return Internal::Adaptor::GetImplementation(*this).Load(url);
319 void VectorAnimationRenderer::SetRenderer(Renderer renderer)
321 Internal::Adaptor::GetImplementation(*this).SetRenderer(renderer);
324 void VectorAnimationRenderer::SetSize(uint32_t width, uint32_t height)
326 Internal::Adaptor::GetImplementation(*this).SetSize(width, height);
329 bool VectorAnimationRenderer::Render(uint32_t frameNumber)
331 return Internal::Adaptor::GetImplementation(*this).Render(frameNumber);
334 uint32_t VectorAnimationRenderer::GetTotalFrameNumber() const
336 return Internal::Adaptor::GetImplementation(*this).GetTotalFrameNumber();
339 float VectorAnimationRenderer::GetFrameRate() const
341 return Internal::Adaptor::GetImplementation(*this).GetFrameRate();
344 void VectorAnimationRenderer::GetDefaultSize(uint32_t& width, uint32_t& height) const
346 Internal::Adaptor::GetImplementation(*this).GetDefaultSize(width, height);
349 void VectorAnimationRenderer::GetLayerInfo(Property::Map& map) const
353 bool VectorAnimationRenderer::GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
355 return Internal::Adaptor::GetImplementation(*this).GetMarkerInfo(marker, startFrame, endFrame);
358 void VectorAnimationRenderer::InvalidateBuffer()
360 return Internal::Adaptor::GetImplementation(*this).InvalidateBuffer();
363 void VectorAnimationRenderer::AddPropertyValueCallback(const std::string& keyPath, VectorProperty property, CallbackBase* callback, int32_t id)
365 Internal::Adaptor::GetImplementation(*this).AddPropertyValueCallback(keyPath, property, callback, id);
368 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
370 return Internal::Adaptor::GetImplementation(*this).UploadCompletedSignal();
377 namespace VectorAnimationRenderer
379 void DelayRendering(uint32_t delay)
381 Dali::Internal::Adaptor::gVectorAnimationRenderer->mDelayTime = delay;
382 Dali::Internal::Adaptor::gVectorAnimationRenderer->mTestFrameDrop = true;
385 uint32_t GetDroppedFrames()
387 return Dali::Internal::Adaptor::gVectorAnimationRenderer->mDroppedFrames;
390 } // namespace VectorAnimationRenderer