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