(Vector) Show the broken image when loading is failed
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-vector-animation-renderer.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali/devel-api/adaptor-framework/vector-animation-renderer.h>
19 #include <dali/public-api/object/base-object.h>
20 #include <toolkit-application.h>
21 #include <toolkit-vector-animation-renderer.h>
22 #include <toolkit-event-thread-callback.h>
23 #include <memory>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 namespace Adaptor
32 {
33
34 class VectorAnimationRenderer: public Dali::BaseObject
35 {
36 public:
37
38   VectorAnimationRenderer()
39   : mUrl(),
40     mRenderer(),
41     mWidth( 0 ),
42     mHeight( 0 ),
43     mPreviousFrame( 0 ),
44     mFrameRate( 60.0f ),
45     mEventThreadCallback( new EventThreadCallback( MakeCallback( this, &VectorAnimationRenderer::OnTriggered ) ) )
46   {
47     mCount++;
48
49     if( mCount == 2 )
50     {
51       mFrameRate = 0.1f;
52     }
53   }
54
55   ~VectorAnimationRenderer()
56   {
57     mCount--;
58   }
59
60   bool Load(const std::string& url)
61   {
62     mUrl = url;
63     if(mUrl == "invalid.json")
64     {
65       return false;
66     }
67     return true;
68   }
69
70   void SetRenderer( Dali::Renderer renderer )
71   {
72     mRenderer = renderer;
73
74     if( mWidth != 0 && mHeight != 0 )
75     {
76       Dali::TextureSet textureSet = mRenderer.GetTextures();
77       Dali::Texture texture = Dali::Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight );
78       textureSet.SetTexture( 0, texture );
79       mUploadCompletedSignal.Emit();
80     }
81   }
82
83   void SetSize( uint32_t width, uint32_t height )
84   {
85     mWidth = width;
86     mHeight = height;
87
88     if( mRenderer )
89     {
90       Dali::TextureSet textureSet = mRenderer.GetTextures();
91       Dali::Texture texture = Dali::Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight );
92       textureSet.SetTexture( 0, texture );
93       mUploadCompletedSignal.Emit();
94     }
95   }
96
97   bool Render( uint32_t frameNumber )
98   {
99     if( mNeedTrigger )
100     {
101       mEventThreadCallback->Trigger();
102       mNeedTrigger = false;
103     }
104
105     if( frameNumber == 1 && mPreviousFrame != frameNumber )
106     {
107       mPreviousFrame = frameNumber;
108       // For test corverage
109       return false;
110     }
111     mPreviousFrame = frameNumber;
112     return true;
113   }
114
115   uint32_t GetTotalFrameNumber() const
116   {
117     return VECTOR_ANIMATION_TOTAL_FRAME_NUMBER;
118   }
119
120   float GetFrameRate() const
121   {
122     return mFrameRate;
123   }
124
125   void GetDefaultSize( uint32_t& width, uint32_t& height ) const
126   {
127     width = 100;
128     height = 100;
129   }
130
131   bool GetMarkerInfo( const std::string& marker, uint32_t& startFrame, uint32_t& endFrame ) const
132   {
133     if( marker.compare( VECTOR_ANIMATION_MARKER_NAME_1 ) == 0 )
134     {
135       startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_1;
136       endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_1;
137     }
138     else if( marker.compare( VECTOR_ANIMATION_MARKER_NAME_2 ) == 0 )
139     {
140       startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_2;
141       endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_2;
142     }
143     else
144     {
145       return false;
146     }
147     return true;
148   }
149
150   Dali::VectorAnimationRenderer::UploadCompletedSignalType& UploadCompletedSignal()
151   {
152     return mUploadCompletedSignal;
153   }
154
155   void OnTriggered()
156   {
157   }
158
159 public:
160
161   static uint32_t mCount;
162   static bool mNeedTrigger;
163
164   std::string mUrl;
165   Dali::Renderer mRenderer;
166   uint32_t mWidth;
167   uint32_t mHeight;
168   uint32_t mPreviousFrame;
169   float mFrameRate;
170   Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
171   std::unique_ptr< EventThreadCallback > mEventThreadCallback;
172 };
173
174 uint32_t VectorAnimationRenderer::mCount = 0;
175 bool VectorAnimationRenderer::mNeedTrigger = true;
176
177 inline VectorAnimationRenderer& GetImplementation( Dali::VectorAnimationRenderer& renderer )
178 {
179   DALI_ASSERT_ALWAYS( renderer && "VectorAnimationRenderer handle is empty." );
180   BaseObject& handle = renderer.GetBaseObject();
181   return static_cast< Internal::Adaptor::VectorAnimationRenderer& >( handle );
182 }
183
184 inline const VectorAnimationRenderer& GetImplementation( const Dali::VectorAnimationRenderer& renderer )
185 {
186   DALI_ASSERT_ALWAYS( renderer && "VectorAnimationRenderer handle is empty." );
187   const BaseObject& handle = renderer.GetBaseObject();
188   return static_cast< const Internal::Adaptor::VectorAnimationRenderer& >( handle );
189 }
190
191 } // namespace Adaptor
192
193 } // namespace Internal
194
195
196 /********************************************************************************/
197 /*********************************  PUBLIC CLASS  *******************************/
198 /********************************************************************************/
199
200 VectorAnimationRenderer VectorAnimationRenderer::New()
201 {
202   Internal::Adaptor::VectorAnimationRenderer* animationRenderer = new Internal::Adaptor::VectorAnimationRenderer();
203
204   return VectorAnimationRenderer( animationRenderer );
205 }
206
207 VectorAnimationRenderer::VectorAnimationRenderer()
208 {
209 }
210
211 VectorAnimationRenderer::~VectorAnimationRenderer()
212 {
213 }
214
215 VectorAnimationRenderer::VectorAnimationRenderer( Internal::Adaptor::VectorAnimationRenderer* internal )
216 : BaseHandle( internal )
217 {
218 }
219
220 VectorAnimationRenderer::VectorAnimationRenderer( const VectorAnimationRenderer& handle )
221 : BaseHandle( handle )
222 {
223 }
224
225 VectorAnimationRenderer& VectorAnimationRenderer::operator=( const VectorAnimationRenderer& rhs )
226 {
227   BaseHandle::operator=( rhs );
228   return *this;
229 }
230
231 void VectorAnimationRenderer::Finalize()
232 {
233 }
234
235 bool VectorAnimationRenderer::Load(const std::string& url)
236 {
237   return Internal::Adaptor::GetImplementation( *this ).Load(url);
238 }
239
240 void VectorAnimationRenderer::SetRenderer( Renderer renderer )
241 {
242   Internal::Adaptor::GetImplementation( *this ).SetRenderer( renderer );
243 }
244
245 void VectorAnimationRenderer::SetSize( uint32_t width, uint32_t height )
246 {
247   Internal::Adaptor::GetImplementation( *this ).SetSize( width, height );
248 }
249
250 bool VectorAnimationRenderer::Render( uint32_t frameNumber )
251 {
252   return Internal::Adaptor::GetImplementation( *this ).Render( frameNumber );
253 }
254
255 uint32_t VectorAnimationRenderer::GetTotalFrameNumber() const
256 {
257   return Internal::Adaptor::GetImplementation( *this ).GetTotalFrameNumber();
258 }
259
260 float VectorAnimationRenderer::GetFrameRate() const
261 {
262   return Internal::Adaptor::GetImplementation( *this ).GetFrameRate();
263 }
264
265 void VectorAnimationRenderer::GetDefaultSize( uint32_t& width, uint32_t& height ) const
266 {
267   Internal::Adaptor::GetImplementation( *this ).GetDefaultSize( width, height );
268 }
269
270 void VectorAnimationRenderer::GetLayerInfo( Property::Map& map ) const
271 {
272 }
273
274 bool VectorAnimationRenderer::GetMarkerInfo( const std::string& marker, uint32_t& startFrame, uint32_t& endFrame ) const
275 {
276   return Internal::Adaptor::GetImplementation( *this ).GetMarkerInfo( marker, startFrame, endFrame );
277 }
278
279 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
280 {
281   return Internal::Adaptor::GetImplementation( *this ).UploadCompletedSignal();
282 }
283
284 } // namespace Dali
285
286 namespace Test
287 {
288 namespace VectorAnimationRenderer
289 {
290
291 void RequestTrigger()
292 {
293   Dali::Internal::Adaptor::VectorAnimationRenderer::mNeedTrigger = true;
294 }
295
296 } // VectorAnimationRenderer
297 } // Test
298