2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "visual-factory-cache.h"
21 #include <dali/devel-api/common/hash.h>
22 #include <dali/public-api/images/resource-image.h>
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>
31 const char * const BROKEN_VISUAL_IMAGE_URL( DALI_IMAGE_DIR "broken.png");
43 VisualFactoryCache::VisualFactoryCache()
44 : mSvgRasterizeThread( NULL )
48 VisualFactoryCache::~VisualFactoryCache()
50 SvgRasterizeThread::TerminateThread( mSvgRasterizeThread );
53 Geometry VisualFactoryCache::GetGeometry( GeometryType type )
55 return mGeometry[type];
58 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
60 mGeometry[type] = geometry;
63 Shader VisualFactoryCache::GetShader( ShaderType type )
68 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
70 mShader[type] = shader;
73 int VisualFactoryCache::FindRenderer( const std::string& key ) const
75 int hash = Dali::CalculateHash( key );
77 HashVector::Iterator startIt = mRendererHashes.Begin();
78 HashVector::Iterator it;
82 it = std::find( startIt, mRendererHashes.End(), hash );
83 if( it != mRendererHashes.End() )
85 int index = it - mRendererHashes.Begin();
86 const CachedRenderer* cachedRenderer = mRenderers[ index ];
88 if( cachedRenderer && cachedRenderer->mKey == key )
103 Renderer VisualFactoryCache::GetRenderer( const std::string& key ) const
105 int index = FindRenderer( key );
108 return mRenderers[ index ]->mRenderer.GetHandle();
116 void VisualFactoryCache::SaveRenderer( const std::string& key, Renderer& renderer )
118 int hash = Dali::CalculateHash( key );
119 const CachedRenderer* cachedRenderer = new CachedRenderer( key, renderer );
121 CachedRenderers::Iterator it = std::find( mRenderers.Begin(), mRenderers.End(), static_cast< CachedRenderer* >( NULL ) );
122 if( it != mRenderers.End() )
124 *it = cachedRenderer;
125 int index = it - mRenderers.Begin();
126 mRendererHashes[ index ] = hash;
130 mRendererHashes.PushBack( hash );
131 mRenderers.PushBack( cachedRenderer );
135 bool VisualFactoryCache::CleanRendererCache( const std::string& key )
137 int index = FindRenderer( key );
140 const CachedRenderer*& cachedRenderer = mRenderers[ index ];
141 if( !cachedRenderer->mRenderer.GetHandle() )
143 mRendererHashes[ index ] = Dali::INITIAL_HASH_VALUE;
145 delete cachedRenderer;
146 cachedRenderer = NULL;
153 void VisualFactoryCache::CacheDebugRenderer( Renderer& renderer )
155 mDebugRenderer = renderer;
158 Renderer VisualFactoryCache::GetDebugRenderer()
160 return mDebugRenderer;
163 Geometry VisualFactoryCache::CreateQuadGeometry()
165 const float halfWidth = 0.5f;
166 const float halfHeight = 0.5f;
167 struct QuadVertex { Vector2 position;};
168 QuadVertex quadVertexData[4] =
170 { Vector2(-halfWidth, -halfHeight) },
171 { Vector2(-halfWidth, halfHeight) },
172 { Vector2( halfWidth, -halfHeight) },
173 { Vector2( halfWidth, halfHeight) }
176 Property::Map quadVertexFormat;
177 quadVertexFormat["aPosition"] = Property::VECTOR2;
178 PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
179 quadVertices.SetData( quadVertexData, 4 );
181 // Create the geometry object
182 Geometry geometry = Geometry::New();
183 geometry.AddVertexBuffer( quadVertices );
184 geometry.SetType( Geometry::TRIANGLE_STRIP );
189 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
193 mAtlasManager = new ImageAtlasManager();
194 mAtlasManager->SetBrokenImage( BROKEN_VISUAL_IMAGE_URL );
197 return mAtlasManager;
200 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
202 if( !mSvgRasterizeThread )
204 mSvgRasterizeThread = new SvgRasterizeThread( new EventThreadCallback( MakeCallback( this, &VisualFactoryCache::ApplyRasterizedSVGToSampler ) ) );
205 mSvgRasterizeThread->Start();
207 return mSvgRasterizeThread;
210 void VisualFactoryCache::ApplyRasterizedSVGToSampler()
212 while( RasterizingTaskPtr task = mSvgRasterizeThread->NextCompletedTask() )
214 task->GetSvgVisual()->ApplyRasterizedImage( task->GetPixelData() );
218 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
220 uint16_t gridWidth = gridSize.GetWidth();
221 uint16_t gridHeight = gridSize.GetHeight();
224 Vector< Vector2 > vertices;
225 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
227 for( int y = 0; y < gridHeight + 1; ++y )
229 for( int x = 0; x < gridWidth + 1; ++x )
231 vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight - 0.5f) );
236 Vector< unsigned short > indices;
237 indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
239 for( unsigned int row = 0u; row < gridHeight; ++row )
241 unsigned int rowStartIndex = row*(gridWidth+1u);
242 unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
244 if( row != 0u ) // degenerate index on non-first row
246 indices.PushBack( rowStartIndex );
249 for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
251 indices.PushBack( rowStartIndex + column);
252 indices.PushBack( nextRowStartIndex + column);
255 if( row != gridHeight-1u ) // degenerate index on non-last row
257 indices.PushBack( nextRowStartIndex + gridWidth );
261 Property::Map vertexFormat;
262 vertexFormat[ "aPosition" ] = Property::VECTOR2;
263 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
264 if( vertices.Size() > 0 )
266 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
269 Property::Map indexFormat;
270 indexFormat[ "indices" ] = Property::INTEGER;
271 PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat );
274 // Create the geometry object
275 Geometry geometry = Geometry::New();
276 geometry.AddVertexBuffer( vertexPropertyBuffer );
277 if( indices.Size() > 0 )
279 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
282 geometry.SetType( Geometry::TRIANGLE_STRIP );
287 Geometry VisualFactoryCache::CreateBatchQuadGeometry( Vector4 texCoords )
289 const float halfWidth = 0.5f;
290 const float halfHeight = 0.5f;
292 QuadVertex( const Vector2& vertexPosition, const Vector2& vertexTexCoords )
293 : position( vertexPosition ),
294 texCoords( vertexTexCoords )
300 // special case, when texture takes whole space
301 if( texCoords == Vector4::ZERO )
303 texCoords = Vector4(0.0f, 0.0f, 1.0f, 1.0f);
306 QuadVertex quadVertexData[6] =
308 QuadVertex( Vector2(-halfWidth, -halfHeight ), Vector2(texCoords.x, texCoords.y) ),
309 QuadVertex( Vector2( halfWidth, -halfHeight ), Vector2(texCoords.z, texCoords.y) ),
310 QuadVertex( Vector2(-halfWidth, halfHeight ), Vector2(texCoords.x, texCoords.w) ),
311 QuadVertex( Vector2( halfWidth, -halfHeight ), Vector2(texCoords.z, texCoords.y) ),
312 QuadVertex( Vector2(-halfWidth, halfHeight ), Vector2(texCoords.x, texCoords.w) ),
313 QuadVertex( Vector2( halfWidth, halfHeight ), Vector2(texCoords.z, texCoords.w) ),
316 Property::Map vertexFormat;
317 vertexFormat[ "aPosition" ] = Property::VECTOR2;
318 vertexFormat[ "aTexCoord" ] = Property::VECTOR2;
319 PropertyBuffer vertexBuffer = PropertyBuffer::New( vertexFormat );
320 vertexBuffer.SetData( quadVertexData, 6 );
322 // create geometry as normal, single quad
323 Geometry geometry = Geometry::New();
324 geometry.AddVertexBuffer( vertexBuffer );
325 geometry.SetType( Geometry::TRIANGLES );
330 Image VisualFactoryCache::GetBrokenVisualImage()
332 return ResourceImage::New( BROKEN_VISUAL_IMAGE_URL );
335 } // namespace Internal
337 } // namespace Toolkit