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/public-api/object/base-object.h>
21 #include <toolkit-application.h>
22 #include <toolkit-event-thread-callback.h>
23 #include <toolkit-vector-animation-renderer.h>
36 Dali::Internal::Adaptor::VectorAnimationRenderer* gVectorAnimationRenderer = nullptr;
39 class VectorAnimationRenderer : public Dali::BaseObject
42 VectorAnimationRenderer()
49 mTotalFrameNumber(VECTOR_ANIMATION_TOTAL_FRAME_NUMBER),
54 mTestFrameDrop(false),
55 mNeedDroppedFrames(false),
56 mEventThreadCallback(new EventThreadCallback(MakeCallback(this, &VectorAnimationRenderer::OnTriggered)))
66 ~VectorAnimationRenderer()
71 bool Load(const std::string& url)
74 if(mUrl == "invalid.json")
79 else if(mUrl == "framedrop.json")
81 // Change total frame number for test
82 mTotalFrameNumber = 200;
83 mTestFrameDrop = true;
92 void SetRenderer(Dali::Renderer renderer)
97 void SetSize(uint32_t width, uint32_t height)
105 mResourceReady = false;
109 bool Render(uint32_t frameNumber)
111 if(mWidth == 0 || mHeight == 0)
118 std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int32_t>(mDelayTime)));
119 mTestFrameDrop = false;
120 mNeedDroppedFrames = true;
122 else if(mNeedDroppedFrames)
124 mDroppedFrames = (frameNumber > mPreviousFrame) ? frameNumber - mPreviousFrame - 1 : frameNumber + (mTotalFrameNumber - mPreviousFrame) - 1;
126 mNeedDroppedFrames = false;
131 mEventThreadCallback->Trigger();
132 mNeedTrigger = false;
135 if(frameNumber == 1 && mPreviousFrame != frameNumber)
137 mPreviousFrame = frameNumber;
138 // For test corverage
141 mPreviousFrame = frameNumber;
145 uint32_t GetTotalFrameNumber() const
147 return mTotalFrameNumber;
150 float GetFrameRate() const
155 void GetDefaultSize(uint32_t& width, uint32_t& height) const
157 width = mDefaultWidth;
158 height = mDefaultHeight;
161 bool GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
163 if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_1) == 0)
165 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_1;
166 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_1;
168 else if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_2) == 0)
170 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_2;
171 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_2;
180 void InvalidateBuffer()
183 mResourceReady = false;
186 Dali::VectorAnimationRenderer::UploadCompletedSignalType& UploadCompletedSignal()
188 return mUploadCompletedSignal;
195 mResourceReady = true;
197 Dali::TextureSet textureSet = mRenderer.GetTextures();
198 Dali::Texture texture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight);
199 textureSet.SetTexture(0, texture);
201 Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(mWidth, mHeight, Pixel::RGBA8888);
202 Dali::PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
203 texture.Upload(pixelData);
205 mUploadCompletedSignal.Emit();
210 static uint32_t mCount;
213 Dali::Renderer mRenderer;
216 uint32_t mDefaultWidth;
217 uint32_t mDefaultHeight;
218 uint32_t mTotalFrameNumber;
219 uint32_t mPreviousFrame;
221 uint32_t mDroppedFrames;
224 bool mNeedDroppedFrames;
225 bool mLoadFailed{false};
226 bool mResourceReady{false};
227 bool mNeedTrigger{true};
229 Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
230 std::unique_ptr<EventThreadCallback> mEventThreadCallback;
233 uint32_t VectorAnimationRenderer::mCount = 0;
235 inline VectorAnimationRenderer& GetImplementation(Dali::VectorAnimationRenderer& renderer)
237 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
238 BaseObject& handle = renderer.GetBaseObject();
239 return static_cast<Internal::Adaptor::VectorAnimationRenderer&>(handle);
242 inline const VectorAnimationRenderer& GetImplementation(const Dali::VectorAnimationRenderer& renderer)
244 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
245 const BaseObject& handle = renderer.GetBaseObject();
246 return static_cast<const Internal::Adaptor::VectorAnimationRenderer&>(handle);
249 } // namespace Adaptor
251 } // namespace Internal
253 /********************************************************************************/
254 /********************************* PUBLIC CLASS *******************************/
255 /********************************************************************************/
257 VectorAnimationRenderer VectorAnimationRenderer::New()
259 Internal::Adaptor::VectorAnimationRenderer* animationRenderer = new Internal::Adaptor::VectorAnimationRenderer();
261 Internal::Adaptor::gVectorAnimationRenderer = animationRenderer;
263 return VectorAnimationRenderer(animationRenderer);
266 VectorAnimationRenderer::VectorAnimationRenderer()
270 VectorAnimationRenderer::~VectorAnimationRenderer()
274 VectorAnimationRenderer::VectorAnimationRenderer(Internal::Adaptor::VectorAnimationRenderer* internal)
275 : BaseHandle(internal)
279 VectorAnimationRenderer::VectorAnimationRenderer(const VectorAnimationRenderer& handle)
284 VectorAnimationRenderer& VectorAnimationRenderer::operator=(const VectorAnimationRenderer& rhs)
286 BaseHandle::operator=(rhs);
290 void VectorAnimationRenderer::Finalize()
294 bool VectorAnimationRenderer::Load(const std::string& url)
296 return Internal::Adaptor::GetImplementation(*this).Load(url);
299 void VectorAnimationRenderer::SetRenderer(Renderer renderer)
301 Internal::Adaptor::GetImplementation(*this).SetRenderer(renderer);
304 void VectorAnimationRenderer::SetSize(uint32_t width, uint32_t height)
306 Internal::Adaptor::GetImplementation(*this).SetSize(width, height);
309 bool VectorAnimationRenderer::Render(uint32_t frameNumber)
311 return Internal::Adaptor::GetImplementation(*this).Render(frameNumber);
314 uint32_t VectorAnimationRenderer::GetTotalFrameNumber() const
316 return Internal::Adaptor::GetImplementation(*this).GetTotalFrameNumber();
319 float VectorAnimationRenderer::GetFrameRate() const
321 return Internal::Adaptor::GetImplementation(*this).GetFrameRate();
324 void VectorAnimationRenderer::GetDefaultSize(uint32_t& width, uint32_t& height) const
326 Internal::Adaptor::GetImplementation(*this).GetDefaultSize(width, height);
329 void VectorAnimationRenderer::GetLayerInfo(Property::Map& map) const
333 bool VectorAnimationRenderer::GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
335 return Internal::Adaptor::GetImplementation(*this).GetMarkerInfo(marker, startFrame, endFrame);
338 void VectorAnimationRenderer::InvalidateBuffer()
340 return Internal::Adaptor::GetImplementation(*this).InvalidateBuffer();
343 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
345 return Internal::Adaptor::GetImplementation(*this).UploadCompletedSignal();
352 namespace VectorAnimationRenderer
354 void DelayRendering(uint32_t delay)
356 Dali::Internal::Adaptor::gVectorAnimationRenderer->mDelayTime = delay;
359 uint32_t GetDroppedFrames()
361 return Dali::Internal::Adaptor::gVectorAnimationRenderer->mDroppedFrames;
364 } // namespace VectorAnimationRenderer