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;
134 mEventThreadCallback->Trigger();
135 mNeedTrigger = false;
138 if(frameNumber == 1 && mPreviousFrame != frameNumber)
140 mPreviousFrame = frameNumber;
141 // For test corverage
144 mPreviousFrame = frameNumber;
148 uint32_t GetTotalFrameNumber() const
150 return mTotalFrameNumber;
153 float GetFrameRate() const
158 void GetDefaultSize(uint32_t& width, uint32_t& height) const
160 width = mDefaultWidth;
161 height = mDefaultHeight;
164 bool GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
166 if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_1) == 0)
168 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_1;
169 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_1;
171 else if(marker.compare(VECTOR_ANIMATION_MARKER_NAME_2) == 0)
173 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_2;
174 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_2;
183 void InvalidateBuffer()
185 Dali::Mutex::ScopedLock lock(mMutex);
189 mResourceReady = false;
193 Dali::VectorAnimationRenderer::UploadCompletedSignalType& UploadCompletedSignal()
195 return mUploadCompletedSignal;
202 mResourceReady = true;
204 Dali::TextureSet textureSet = mRenderer.GetTextures();
205 Dali::Texture texture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight);
206 textureSet.SetTexture(0, texture);
208 Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(mWidth, mHeight, Pixel::RGBA8888);
209 Dali::PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
210 texture.Upload(pixelData);
212 mUploadCompletedSignal.Emit();
217 static uint32_t mCount;
220 Dali::Renderer mRenderer;
224 uint32_t mDefaultWidth;
225 uint32_t mDefaultHeight;
226 uint32_t mTotalFrameNumber;
227 uint32_t mPreviousFrame;
229 uint32_t mDroppedFrames;
232 bool mNeedDroppedFrames;
233 bool mLoadFailed{false};
234 bool mResourceReady{false};
235 bool mNeedTrigger{true};
237 Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
238 std::unique_ptr<EventThreadCallback> mEventThreadCallback;
241 uint32_t VectorAnimationRenderer::mCount = 0;
243 inline VectorAnimationRenderer& GetImplementation(Dali::VectorAnimationRenderer& renderer)
245 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
246 BaseObject& handle = renderer.GetBaseObject();
247 return static_cast<Internal::Adaptor::VectorAnimationRenderer&>(handle);
250 inline const VectorAnimationRenderer& GetImplementation(const Dali::VectorAnimationRenderer& renderer)
252 DALI_ASSERT_ALWAYS(renderer && "VectorAnimationRenderer handle is empty.");
253 const BaseObject& handle = renderer.GetBaseObject();
254 return static_cast<const Internal::Adaptor::VectorAnimationRenderer&>(handle);
257 } // namespace Adaptor
259 } // namespace Internal
261 /********************************************************************************/
262 /********************************* PUBLIC CLASS *******************************/
263 /********************************************************************************/
265 VectorAnimationRenderer VectorAnimationRenderer::New()
267 Internal::Adaptor::VectorAnimationRenderer* animationRenderer = new Internal::Adaptor::VectorAnimationRenderer();
269 Internal::Adaptor::gVectorAnimationRenderer = animationRenderer;
271 return VectorAnimationRenderer(animationRenderer);
274 VectorAnimationRenderer::VectorAnimationRenderer()
278 VectorAnimationRenderer::~VectorAnimationRenderer()
282 VectorAnimationRenderer::VectorAnimationRenderer(Internal::Adaptor::VectorAnimationRenderer* internal)
283 : BaseHandle(internal)
287 VectorAnimationRenderer::VectorAnimationRenderer(const VectorAnimationRenderer& handle)
292 VectorAnimationRenderer& VectorAnimationRenderer::operator=(const VectorAnimationRenderer& rhs)
294 BaseHandle::operator=(rhs);
298 void VectorAnimationRenderer::Finalize()
302 bool VectorAnimationRenderer::Load(const std::string& url)
304 return Internal::Adaptor::GetImplementation(*this).Load(url);
307 void VectorAnimationRenderer::SetRenderer(Renderer renderer)
309 Internal::Adaptor::GetImplementation(*this).SetRenderer(renderer);
312 void VectorAnimationRenderer::SetSize(uint32_t width, uint32_t height)
314 Internal::Adaptor::GetImplementation(*this).SetSize(width, height);
317 bool VectorAnimationRenderer::Render(uint32_t frameNumber)
319 return Internal::Adaptor::GetImplementation(*this).Render(frameNumber);
322 uint32_t VectorAnimationRenderer::GetTotalFrameNumber() const
324 return Internal::Adaptor::GetImplementation(*this).GetTotalFrameNumber();
327 float VectorAnimationRenderer::GetFrameRate() const
329 return Internal::Adaptor::GetImplementation(*this).GetFrameRate();
332 void VectorAnimationRenderer::GetDefaultSize(uint32_t& width, uint32_t& height) const
334 Internal::Adaptor::GetImplementation(*this).GetDefaultSize(width, height);
337 void VectorAnimationRenderer::GetLayerInfo(Property::Map& map) const
341 bool VectorAnimationRenderer::GetMarkerInfo(const std::string& marker, uint32_t& startFrame, uint32_t& endFrame) const
343 return Internal::Adaptor::GetImplementation(*this).GetMarkerInfo(marker, startFrame, endFrame);
346 void VectorAnimationRenderer::InvalidateBuffer()
348 return Internal::Adaptor::GetImplementation(*this).InvalidateBuffer();
351 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
353 return Internal::Adaptor::GetImplementation(*this).UploadCompletedSignal();
360 namespace VectorAnimationRenderer
362 void DelayRendering(uint32_t delay)
364 Dali::Internal::Adaptor::gVectorAnimationRenderer->mDelayTime = delay;
365 Dali::Internal::Adaptor::gVectorAnimationRenderer->mTestFrameDrop = true;
368 uint32_t GetDroppedFrames()
370 return Dali::Internal::Adaptor::gVectorAnimationRenderer->mDroppedFrames;
373 } // namespace VectorAnimationRenderer