Merge "Dali C# binding : Creating Color Constructor to accept enum Colors" into devel...
[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
30 {
31 const char * const BROKEN_VISUAL_IMAGE_URL( DALI_IMAGE_DIR "broken.png");
32 }
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 VisualFactoryCache::VisualFactoryCache()
44 : mSvgRasterizeThread( NULL )
45 {
46 }
47
48 VisualFactoryCache::~VisualFactoryCache()
49 {
50   SvgRasterizeThread::TerminateThread( mSvgRasterizeThread );
51 }
52
53 Geometry VisualFactoryCache::GetGeometry( GeometryType type )
54 {
55   return mGeometry[type];
56 }
57
58 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
59 {
60   mGeometry[type] = geometry;
61 }
62
63 Shader VisualFactoryCache::GetShader( ShaderType type )
64 {
65   return mShader[type];
66 }
67
68 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
69 {
70   mShader[type] = shader;
71 }
72
73 int VisualFactoryCache::FindRenderer( const std::string& key ) const
74 {
75   int hash = Dali::CalculateHash( key );
76
77   HashVector::Iterator startIt = mRendererHashes.Begin();
78   HashVector::Iterator it;
79
80   for(;;)
81   {
82     it = std::find( startIt, mRendererHashes.End(), hash );
83     if( it != mRendererHashes.End() )
84     {
85       int index = it - mRendererHashes.Begin();
86       const CachedRenderer* cachedRenderer = mRenderers[ index ];
87
88       if( cachedRenderer && cachedRenderer->mKey == key )
89       {
90         return index;
91       }
92     }
93     else
94     {
95       break;
96     }
97     startIt = it + 1;
98   }
99
100   return -1;
101 }
102
103 Renderer VisualFactoryCache::GetRenderer( const std::string& key ) const
104 {
105   int index = FindRenderer( key );
106   if( index != -1 )
107   {
108     return mRenderers[ index ]->mRenderer.GetHandle();
109   }
110   else
111   {
112     return Renderer();
113   }
114 }
115
116 void VisualFactoryCache::SaveRenderer( const std::string& key, Renderer& renderer )
117 {
118   int hash = Dali::CalculateHash( key );
119   const CachedRenderer* cachedRenderer = new CachedRenderer( key, renderer );
120
121   CachedRenderers::Iterator it = std::find( mRenderers.Begin(), mRenderers.End(), static_cast< CachedRenderer* >( NULL ) );
122   if( it != mRenderers.End() )
123   {
124     *it = cachedRenderer;
125     int index = it - mRenderers.Begin();
126     mRendererHashes[ index ] = hash;
127   }
128   else
129   {
130     mRendererHashes.PushBack( hash );
131     mRenderers.PushBack( cachedRenderer );
132   }
133 }
134
135 bool VisualFactoryCache::CleanRendererCache( const std::string& key )
136 {
137   int index = FindRenderer( key );
138   if( index != -1 )
139   {
140     const CachedRenderer*& cachedRenderer = mRenderers[ index ];
141     if( !cachedRenderer->mRenderer.GetHandle() )
142     {
143       mRendererHashes[ index ] = Dali::INITIAL_HASH_VALUE;
144
145       delete cachedRenderer;
146       cachedRenderer = NULL;
147       return true;
148     }
149   }
150   return false;
151 }
152
153 void VisualFactoryCache::CacheWireframeRenderer( Renderer& renderer )
154 {
155   mWireframeRenderer = renderer;
156 }
157
158 Renderer VisualFactoryCache::GetWireframeRenderer()
159 {
160   return mWireframeRenderer;
161 }
162
163 Geometry VisualFactoryCache::CreateQuadGeometry()
164 {
165   const float halfWidth = 0.5f;
166   const float halfHeight = 0.5f;
167   struct QuadVertex { Vector2 position;};
168   QuadVertex quadVertexData[4] =
169   {
170       { Vector2(-halfWidth, -halfHeight) },
171       { Vector2(-halfWidth, halfHeight)  },
172       { Vector2( halfWidth, -halfHeight) },
173       { Vector2( halfWidth, halfHeight)  }
174   };
175
176   Property::Map quadVertexFormat;
177   quadVertexFormat["aPosition"] = Property::VECTOR2;
178   PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
179   quadVertices.SetData( quadVertexData, 4 );
180
181   // Create the geometry object
182   Geometry geometry = Geometry::New();
183   geometry.AddVertexBuffer( quadVertices );
184   geometry.SetType( Geometry::TRIANGLE_STRIP );
185
186   return geometry;
187 }
188
189 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
190 {
191   if( !mAtlasManager )
192   {
193     mAtlasManager = new ImageAtlasManager();
194     mAtlasManager->SetBrokenImage( BROKEN_VISUAL_IMAGE_URL );
195   }
196
197   return mAtlasManager;
198 }
199
200 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
201 {
202   return mNPatchLoader;
203 }
204
205 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
206 {
207   if( !mSvgRasterizeThread )
208   {
209     mSvgRasterizeThread = new SvgRasterizeThread( new EventThreadCallback( MakeCallback( this, &VisualFactoryCache::ApplyRasterizedSVGToSampler ) ) );
210     mSvgRasterizeThread->Start();
211   }
212   return mSvgRasterizeThread;
213 }
214
215 void VisualFactoryCache::ApplyRasterizedSVGToSampler()
216 {
217   while( RasterizingTaskPtr task = mSvgRasterizeThread->NextCompletedTask() )
218   {
219     task->GetSvgVisual()->ApplyRasterizedImage( task->GetPixelData() );
220   }
221 }
222
223 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
224 {
225   uint16_t gridWidth = gridSize.GetWidth();
226   uint16_t gridHeight = gridSize.GetHeight();
227
228   // Create vertices
229   Vector< Vector2 > vertices;
230   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
231
232   for( int y = 0; y < gridHeight + 1; ++y )
233   {
234     for( int x = 0; x < gridWidth + 1; ++x )
235     {
236       vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight  - 0.5f) );
237     }
238   }
239
240   // Create indices
241   Vector< unsigned short > indices;
242   indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
243
244   for( unsigned int row = 0u; row < gridHeight; ++row )
245   {
246     unsigned int rowStartIndex = row*(gridWidth+1u);
247     unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
248
249     if( row != 0u ) // degenerate index on non-first row
250     {
251       indices.PushBack( rowStartIndex );
252     }
253
254     for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
255     {
256       indices.PushBack( rowStartIndex + column);
257       indices.PushBack( nextRowStartIndex + column);
258     }
259
260     if( row != gridHeight-1u ) // degenerate index on non-last row
261     {
262       indices.PushBack( nextRowStartIndex + gridWidth );
263     }
264   }
265
266   Property::Map vertexFormat;
267   vertexFormat[ "aPosition" ] = Property::VECTOR2;
268   PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
269   if( vertices.Size() > 0 )
270   {
271     vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
272   }
273
274   Property::Map indexFormat;
275   indexFormat[ "indices" ] = Property::INTEGER;
276   PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat );
277
278
279   // Create the geometry object
280   Geometry geometry = Geometry::New();
281   geometry.AddVertexBuffer( vertexPropertyBuffer );
282   if( indices.Size() > 0 )
283   {
284     geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
285   }
286
287   geometry.SetType( Geometry::TRIANGLE_STRIP );
288
289   return geometry;
290 }
291
292 Geometry VisualFactoryCache::CreateBatchQuadGeometry( Vector4 texCoords )
293 {
294   const float halfWidth = 0.5f;
295   const float halfHeight = 0.5f;
296   struct QuadVertex {
297     QuadVertex( const Vector2& vertexPosition, const Vector2& vertexTexCoords )
298       : position( vertexPosition ),
299         texCoords( vertexTexCoords )
300     {}
301     Vector2 position;
302     Vector2 texCoords;
303   };
304
305   // special case, when texture takes whole space
306   if( texCoords == Vector4::ZERO )
307   {
308     texCoords = Vector4(0.0f, 0.0f, 1.0f, 1.0f);
309   }
310
311   QuadVertex quadVertexData[6] =
312   {
313     QuadVertex( Vector2(-halfWidth,   -halfHeight ),   Vector2(texCoords.x, texCoords.y) ),
314     QuadVertex( Vector2( halfWidth,   -halfHeight ),   Vector2(texCoords.z, texCoords.y) ),
315     QuadVertex( Vector2(-halfWidth,    halfHeight ),   Vector2(texCoords.x, texCoords.w) ),
316     QuadVertex( Vector2( halfWidth,   -halfHeight ),   Vector2(texCoords.z, texCoords.y) ),
317     QuadVertex( Vector2(-halfWidth,    halfHeight ),   Vector2(texCoords.x, texCoords.w) ),
318     QuadVertex( Vector2( halfWidth,    halfHeight ),   Vector2(texCoords.z, texCoords.w) ),
319   };
320
321   Property::Map vertexFormat;
322   vertexFormat[ "aPosition" ] = Property::VECTOR2;
323   vertexFormat[ "aTexCoord" ] = Property::VECTOR2;
324   PropertyBuffer vertexBuffer = PropertyBuffer::New( vertexFormat );
325   vertexBuffer.SetData( quadVertexData, 6 );
326
327   // create geometry as normal, single quad
328   Geometry geometry = Geometry::New();
329   geometry.AddVertexBuffer( vertexBuffer );
330   geometry.SetType( Geometry::TRIANGLES );
331
332   return geometry;
333 }
334
335 Image VisualFactoryCache::GetBrokenVisualImage()
336 {
337   return ResourceImage::New( BROKEN_VISUAL_IMAGE_URL );
338 }
339
340 } // namespace Internal
341
342 } // namespace Toolkit
343
344 } // namespace Dali
345