Support animation of Visual transform properties
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-factory-cache.cpp
1  /*
2  * Copyright (c) 2020 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 // CLASS HEADER
18 #include "visual-factory-cache.h"
19
20 // EXTERNAL INCLUDES
21 #include <dali/devel-api/common/hash.h>
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/visuals/color/color-visual.h>
26 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
27 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
28 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-manager.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 VisualFactoryCache::VisualFactoryCache( bool preMultiplyOnLoad )
40 : mSvgRasterizeThread( NULL ),
41   mVectorAnimationManager(),
42   mBrokenImageUrl(""),
43   mPreMultiplyOnLoad( preMultiplyOnLoad )
44 {
45 }
46
47 VisualFactoryCache::~VisualFactoryCache()
48 {
49   SvgRasterizeThread::TerminateThread( mSvgRasterizeThread );
50 }
51
52 Geometry VisualFactoryCache::GetGeometry( GeometryType type )
53 {
54   if( !mGeometry[type] && type == QUAD_GEOMETRY )
55   {
56     mGeometry[type] = CreateQuadGeometry();
57   }
58
59   return mGeometry[type];
60 }
61
62 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
63 {
64   mGeometry[type] = geometry;
65 }
66
67 Shader VisualFactoryCache::GetShader( ShaderType type )
68 {
69   return mShader[type];
70 }
71
72 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
73 {
74   mShader[type] = shader;
75 }
76
77 Geometry VisualFactoryCache::CreateQuadGeometry()
78 {
79   const float halfWidth = 0.5f;
80   const float halfHeight = 0.5f;
81   struct QuadVertex { Vector2 position;};
82   QuadVertex quadVertexData[4] =
83   {
84       { Vector2(-halfWidth, -halfHeight) },
85       { Vector2(-halfWidth, halfHeight)  },
86       { Vector2( halfWidth, -halfHeight) },
87       { Vector2( halfWidth, halfHeight)  }
88   };
89
90   Property::Map quadVertexFormat;
91   quadVertexFormat["aPosition"] = Property::VECTOR2;
92   VertexBuffer quadVertices = VertexBuffer::New( quadVertexFormat );
93   quadVertices.SetData( quadVertexData, 4 );
94
95   // Create the geometry object
96   Geometry geometry = Geometry::New();
97   geometry.AddVertexBuffer( quadVertices );
98   geometry.SetType( Geometry::TRIANGLE_STRIP );
99
100   return geometry;
101 }
102
103 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
104 {
105   if( !mAtlasManager )
106   {
107     mAtlasManager = new ImageAtlasManager();
108     mAtlasManager->SetBrokenImage( mBrokenImageUrl );
109   }
110
111   return mAtlasManager;
112 }
113
114 TextureManager& VisualFactoryCache::GetTextureManager()
115 {
116   return mTextureManager;
117 }
118
119 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
120 {
121   return mNPatchLoader;
122 }
123
124 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
125 {
126   if( !mSvgRasterizeThread )
127   {
128     mSvgRasterizeThread = new SvgRasterizeThread();
129     mSvgRasterizeThread->Start();
130   }
131   return mSvgRasterizeThread;
132 }
133
134 VectorAnimationManager& VisualFactoryCache::GetVectorAnimationManager()
135 {
136   if( !mVectorAnimationManager )
137   {
138     mVectorAnimationManager = std::unique_ptr< VectorAnimationManager >( new VectorAnimationManager() );
139   }
140   return *mVectorAnimationManager;
141 }
142
143 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
144 {
145   uint16_t gridWidth = gridSize.GetWidth();
146   uint16_t gridHeight = gridSize.GetHeight();
147
148   // Create vertices
149   Vector< Vector2 > vertices;
150   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
151
152   for( int y = 0; y < gridHeight + 1; ++y )
153   {
154     for( int x = 0; x < gridWidth + 1; ++x )
155     {
156       vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight  - 0.5f) );
157     }
158   }
159
160   // Create indices
161   Vector< unsigned short > indices;
162   indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
163
164   for( unsigned int row = 0u; row < gridHeight; ++row )
165   {
166     unsigned int rowStartIndex = row*(gridWidth+1u);
167     unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
168
169     if( row != 0u ) // degenerate index on non-first row
170     {
171       indices.PushBack( rowStartIndex );
172     }
173
174     for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
175     {
176       indices.PushBack( rowStartIndex + column);
177       indices.PushBack( nextRowStartIndex + column);
178     }
179
180     if( row != gridHeight-1u ) // degenerate index on non-last row
181     {
182       indices.PushBack( nextRowStartIndex + gridWidth );
183     }
184   }
185
186   Property::Map vertexFormat;
187   vertexFormat[ "aPosition" ] = Property::VECTOR2;
188   VertexBuffer vertexBuffer = VertexBuffer::New( vertexFormat );
189   if( vertices.Size() > 0 )
190   {
191     vertexBuffer.SetData( &vertices[ 0 ], vertices.Size() );
192   }
193
194   Property::Map indexFormat;
195   indexFormat[ "indices" ] = Property::INTEGER;
196   VertexBuffer indexVertexBuffer = VertexBuffer::New( indexFormat );
197
198
199   // Create the geometry object
200   Geometry geometry = Geometry::New();
201   geometry.AddVertexBuffer( vertexBuffer );
202   if( indices.Size() > 0 )
203   {
204     geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
205   }
206
207   geometry.SetType( Geometry::TRIANGLE_STRIP );
208
209   return geometry;
210 }
211
212 Texture VisualFactoryCache::GetBrokenVisualImage()
213 {
214   if(!mBrokenImageTexture && mBrokenImageUrl.size())
215   {
216     PixelData data;
217     Devel::PixelBuffer pixelBuffer = LoadImageFromFile( mBrokenImageUrl );
218     if( pixelBuffer )
219     {
220       data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
221       mBrokenImageTexture = Texture::New( Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(),
222                                       data.GetWidth(), data.GetHeight() );
223       mBrokenImageTexture.Upload( data );
224     }
225   }
226   return mBrokenImageTexture;
227 }
228
229 void VisualFactoryCache::SetPreMultiplyOnLoad( bool preMultiply )
230 {
231   mPreMultiplyOnLoad = preMultiply;
232 }
233
234 bool VisualFactoryCache::GetPreMultiplyOnLoad()
235 {
236   return mPreMultiplyOnLoad;
237 }
238
239 void VisualFactoryCache::SetBrokenImageUrl(const std::string& brokenImageUrl)
240 {
241   mBrokenImageUrl = brokenImageUrl;
242
243   if( !mAtlasManager )
244   {
245     mAtlasManager = new ImageAtlasManager();
246   }
247
248   mAtlasManager->SetBrokenImage( mBrokenImageUrl );
249   mTextureManager.SetBrokenImageUrl( mBrokenImageUrl );
250 }
251
252 } // namespace Internal
253
254 } // namespace Toolkit
255
256 } // namespace Dali