Merge "Remove batching." 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
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   if( !mGeometry[type] && type == QUAD_GEOMETRY )
56   {
57     mGeometry[type] = CreateQuadGeometry();
58   }
59
60   return mGeometry[type];
61 }
62
63 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
64 {
65   mGeometry[type] = geometry;
66 }
67
68 Shader VisualFactoryCache::GetShader( ShaderType type )
69 {
70   return mShader[type];
71 }
72
73 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
74 {
75   mShader[type] = shader;
76 }
77
78 int VisualFactoryCache::FindRenderer( const std::string& key ) const
79 {
80   int hash = Dali::CalculateHash( key );
81
82   HashVector::Iterator startIt = mRendererHashes.Begin();
83   HashVector::Iterator it;
84
85   for(;;)
86   {
87     it = std::find( startIt, mRendererHashes.End(), hash );
88     if( it != mRendererHashes.End() )
89     {
90       int index = it - mRendererHashes.Begin();
91       const CachedRenderer* cachedRenderer = mRenderers[ index ];
92
93       if( cachedRenderer && cachedRenderer->mKey == key )
94       {
95         return index;
96       }
97     }
98     else
99     {
100       break;
101     }
102     startIt = it + 1;
103   }
104
105   return -1;
106 }
107
108 Renderer VisualFactoryCache::GetRenderer( const std::string& key ) const
109 {
110   int index = FindRenderer( key );
111   if( index != -1 )
112   {
113     return mRenderers[ index ]->mRenderer.GetHandle();
114   }
115   else
116   {
117     return Renderer();
118   }
119 }
120
121 void VisualFactoryCache::SaveRenderer( const std::string& key, Renderer& renderer )
122 {
123   int hash = Dali::CalculateHash( key );
124   const CachedRenderer* cachedRenderer = new CachedRenderer( key, renderer );
125
126   CachedRenderers::Iterator it = std::find( mRenderers.Begin(), mRenderers.End(), static_cast< CachedRenderer* >( NULL ) );
127   if( it != mRenderers.End() )
128   {
129     *it = cachedRenderer;
130     int index = it - mRenderers.Begin();
131     mRendererHashes[ index ] = hash;
132   }
133   else
134   {
135     mRendererHashes.PushBack( hash );
136     mRenderers.PushBack( cachedRenderer );
137   }
138 }
139
140 bool VisualFactoryCache::CleanRendererCache( const std::string& key )
141 {
142   int index = FindRenderer( key );
143   if( index != -1 )
144   {
145     const CachedRenderer*& cachedRenderer = mRenderers[ index ];
146     if( !cachedRenderer->mRenderer.GetHandle() )
147     {
148       mRendererHashes[ index ] = Dali::INITIAL_HASH_VALUE;
149
150       delete cachedRenderer;
151       cachedRenderer = NULL;
152       return true;
153     }
154   }
155   return false;
156 }
157
158 Geometry VisualFactoryCache::CreateQuadGeometry()
159 {
160   const float halfWidth = 0.5f;
161   const float halfHeight = 0.5f;
162   struct QuadVertex { Vector2 position;};
163   QuadVertex quadVertexData[4] =
164   {
165       { Vector2(-halfWidth, -halfHeight) },
166       { Vector2(-halfWidth, halfHeight)  },
167       { Vector2( halfWidth, -halfHeight) },
168       { Vector2( halfWidth, halfHeight)  }
169   };
170
171   Property::Map quadVertexFormat;
172   quadVertexFormat["aPosition"] = Property::VECTOR2;
173   PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
174   quadVertices.SetData( quadVertexData, 4 );
175
176   // Create the geometry object
177   Geometry geometry = Geometry::New();
178   geometry.AddVertexBuffer( quadVertices );
179   geometry.SetType( Geometry::TRIANGLE_STRIP );
180
181   return geometry;
182 }
183
184 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
185 {
186   if( !mAtlasManager )
187   {
188     mAtlasManager = new ImageAtlasManager();
189     mAtlasManager->SetBrokenImage( BROKEN_VISUAL_IMAGE_URL );
190   }
191
192   return mAtlasManager;
193 }
194
195 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
196 {
197   return mNPatchLoader;
198 }
199
200 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
201 {
202   if( !mSvgRasterizeThread )
203   {
204     mSvgRasterizeThread = new SvgRasterizeThread( new EventThreadCallback( MakeCallback( this, &VisualFactoryCache::ApplyRasterizedSVGToSampler ) ) );
205     mSvgRasterizeThread->Start();
206   }
207   return mSvgRasterizeThread;
208 }
209
210 void VisualFactoryCache::ApplyRasterizedSVGToSampler()
211 {
212   while( RasterizingTaskPtr task = mSvgRasterizeThread->NextCompletedTask() )
213   {
214     task->GetSvgVisual()->ApplyRasterizedImage( task->GetPixelData() );
215   }
216 }
217
218 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
219 {
220   uint16_t gridWidth = gridSize.GetWidth();
221   uint16_t gridHeight = gridSize.GetHeight();
222
223   // Create vertices
224   Vector< Vector2 > vertices;
225   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
226
227   for( int y = 0; y < gridHeight + 1; ++y )
228   {
229     for( int x = 0; x < gridWidth + 1; ++x )
230     {
231       vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight  - 0.5f) );
232     }
233   }
234
235   // Create indices
236   Vector< unsigned short > indices;
237   indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
238
239   for( unsigned int row = 0u; row < gridHeight; ++row )
240   {
241     unsigned int rowStartIndex = row*(gridWidth+1u);
242     unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
243
244     if( row != 0u ) // degenerate index on non-first row
245     {
246       indices.PushBack( rowStartIndex );
247     }
248
249     for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
250     {
251       indices.PushBack( rowStartIndex + column);
252       indices.PushBack( nextRowStartIndex + column);
253     }
254
255     if( row != gridHeight-1u ) // degenerate index on non-last row
256     {
257       indices.PushBack( nextRowStartIndex + gridWidth );
258     }
259   }
260
261   Property::Map vertexFormat;
262   vertexFormat[ "aPosition" ] = Property::VECTOR2;
263   PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
264   if( vertices.Size() > 0 )
265   {
266     vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
267   }
268
269   Property::Map indexFormat;
270   indexFormat[ "indices" ] = Property::INTEGER;
271   PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat );
272
273
274   // Create the geometry object
275   Geometry geometry = Geometry::New();
276   geometry.AddVertexBuffer( vertexPropertyBuffer );
277   if( indices.Size() > 0 )
278   {
279     geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
280   }
281
282   geometry.SetType( Geometry::TRIANGLE_STRIP );
283
284   return geometry;
285 }
286
287 Image VisualFactoryCache::GetBrokenVisualImage()
288 {
289   return ResourceImage::New( BROKEN_VISUAL_IMAGE_URL );
290 }
291
292 } // namespace Internal
293
294 } // namespace Toolkit
295
296 } // namespace Dali
297