Merge "fixed align problems when whitespaces are included" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-factory-cache.cpp
1  /*
2  * Copyright (c) 2016 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/public-api/images/resource-image.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
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 VisualFactoryCache::VisualFactoryCache( bool preMultiplyOnLoad )
39 : mSvgRasterizeThread( NULL ),
40   mVectorAnimationThread(),
41   mBrokenImageUrl(""),
42   mPreMultiplyOnLoad( preMultiplyOnLoad )
43 {
44 }
45
46 VisualFactoryCache::~VisualFactoryCache()
47 {
48   SvgRasterizeThread::TerminateThread( mSvgRasterizeThread );
49 }
50
51 Geometry VisualFactoryCache::GetGeometry( GeometryType type )
52 {
53   if( !mGeometry[type] && type == QUAD_GEOMETRY )
54   {
55     mGeometry[type] = CreateQuadGeometry();
56   }
57
58   return mGeometry[type];
59 }
60
61 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
62 {
63   mGeometry[type] = geometry;
64 }
65
66 Shader VisualFactoryCache::GetShader( ShaderType type )
67 {
68   return mShader[type];
69 }
70
71 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
72 {
73   mShader[type] = shader;
74 }
75
76 Geometry VisualFactoryCache::CreateQuadGeometry()
77 {
78   const float halfWidth = 0.5f;
79   const float halfHeight = 0.5f;
80   struct QuadVertex { Vector2 position;};
81   QuadVertex quadVertexData[4] =
82   {
83       { Vector2(-halfWidth, -halfHeight) },
84       { Vector2(-halfWidth, halfHeight)  },
85       { Vector2( halfWidth, -halfHeight) },
86       { Vector2( halfWidth, halfHeight)  }
87   };
88
89   Property::Map quadVertexFormat;
90   quadVertexFormat["aPosition"] = Property::VECTOR2;
91   PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
92   quadVertices.SetData( quadVertexData, 4 );
93
94   // Create the geometry object
95   Geometry geometry = Geometry::New();
96   geometry.AddVertexBuffer( quadVertices );
97   geometry.SetType( Geometry::TRIANGLE_STRIP );
98
99   return geometry;
100 }
101
102 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
103 {
104   if( !mAtlasManager )
105   {
106     mAtlasManager = new ImageAtlasManager();
107     mAtlasManager->SetBrokenImage( mBrokenImageUrl );
108   }
109
110   return mAtlasManager;
111 }
112
113 TextureManager& VisualFactoryCache::GetTextureManager()
114 {
115   return mTextureManager;
116 }
117
118 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
119 {
120   return mNPatchLoader;
121 }
122
123 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
124 {
125   if( !mSvgRasterizeThread )
126   {
127     mSvgRasterizeThread = new SvgRasterizeThread( new EventThreadCallback( MakeCallback( this, &VisualFactoryCache::ApplyRasterizedSVGToSampler ) ) );
128     mSvgRasterizeThread->Start();
129   }
130   return mSvgRasterizeThread;
131 }
132
133 VectorAnimationThread& VisualFactoryCache::GetVectorAnimationThread()
134 {
135   if( !mVectorAnimationThread )
136   {
137     mVectorAnimationThread = std::unique_ptr< VectorAnimationThread >( new VectorAnimationThread() );
138     mVectorAnimationThread->Start();
139   }
140   return *mVectorAnimationThread;
141 }
142
143 void VisualFactoryCache::ApplyRasterizedSVGToSampler()
144 {
145   while( RasterizingTaskPtr task = mSvgRasterizeThread->NextCompletedTask() )
146   {
147     task->GetSvgVisual()->ApplyRasterizedImage( task->GetParsedImage(), task->GetPixelData() );
148   }
149 }
150
151 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
152 {
153   uint16_t gridWidth = gridSize.GetWidth();
154   uint16_t gridHeight = gridSize.GetHeight();
155
156   // Create vertices
157   Vector< Vector2 > vertices;
158   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
159
160   for( int y = 0; y < gridHeight + 1; ++y )
161   {
162     for( int x = 0; x < gridWidth + 1; ++x )
163     {
164       vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight  - 0.5f) );
165     }
166   }
167
168   // Create indices
169   Vector< unsigned short > indices;
170   indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
171
172   for( unsigned int row = 0u; row < gridHeight; ++row )
173   {
174     unsigned int rowStartIndex = row*(gridWidth+1u);
175     unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
176
177     if( row != 0u ) // degenerate index on non-first row
178     {
179       indices.PushBack( rowStartIndex );
180     }
181
182     for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
183     {
184       indices.PushBack( rowStartIndex + column);
185       indices.PushBack( nextRowStartIndex + column);
186     }
187
188     if( row != gridHeight-1u ) // degenerate index on non-last row
189     {
190       indices.PushBack( nextRowStartIndex + gridWidth );
191     }
192   }
193
194   Property::Map vertexFormat;
195   vertexFormat[ "aPosition" ] = Property::VECTOR2;
196   PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
197   if( vertices.Size() > 0 )
198   {
199     vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
200   }
201
202   Property::Map indexFormat;
203   indexFormat[ "indices" ] = Property::INTEGER;
204   PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat );
205
206
207   // Create the geometry object
208   Geometry geometry = Geometry::New();
209   geometry.AddVertexBuffer( vertexPropertyBuffer );
210   if( indices.Size() > 0 )
211   {
212     geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
213   }
214
215   geometry.SetType( Geometry::TRIANGLE_STRIP );
216
217   return geometry;
218 }
219
220 Image VisualFactoryCache::GetBrokenVisualImage()
221 {
222   return ResourceImage::New( mBrokenImageUrl );
223 }
224
225 void VisualFactoryCache::SetPreMultiplyOnLoad( bool preMultiply )
226 {
227   mPreMultiplyOnLoad = preMultiply;
228 }
229
230 bool VisualFactoryCache::GetPreMultiplyOnLoad()
231 {
232   return mPreMultiplyOnLoad;
233 }
234
235 void VisualFactoryCache::SetBrokenImageUrl(const std::string& brokenImageUrl)
236 {
237   mBrokenImageUrl = brokenImageUrl;
238
239   if( !mAtlasManager )
240   {
241     mAtlasManager = new ImageAtlasManager();
242   }
243
244   mAtlasManager->SetBrokenImage( mBrokenImageUrl );
245   mTextureManager.SetBrokenImageUrl( mBrokenImageUrl );
246 }
247
248 } // namespace Internal
249
250 } // namespace Toolkit
251
252 } // namespace Dali