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>
24 #include <dali-toolkit/internal/controls/renderers/color/color-visual.h>
25 #include <dali-toolkit/internal/controls/renderers/svg/svg-visual.h>
36 VisualFactoryCache::VisualFactoryCache()
37 : mSvgRasterizeThread( NULL )
41 VisualFactoryCache::~VisualFactoryCache()
43 SvgRasterizeThread::TerminateThread( mSvgRasterizeThread );
46 Geometry VisualFactoryCache::GetGeometry( GeometryType type )
48 return mGeometry[type];
51 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
53 mGeometry[type] = geometry;
56 Shader VisualFactoryCache::GetShader( ShaderType type )
61 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
63 mShader[type] = shader;
66 int VisualFactoryCache::FindRenderer( const std::string& key ) const
68 int hash = Dali::CalculateHash( key );
70 HashVector::Iterator startIt = mRendererHashes.Begin();
71 HashVector::Iterator it;
75 it = std::find( startIt, mRendererHashes.End(), hash );
76 if( it != mRendererHashes.End() )
78 int index = it - mRendererHashes.Begin();
79 const CachedRenderer* cachedRenderer = mRenderers[ index ];
81 if( cachedRenderer && cachedRenderer->mKey == key )
96 Renderer VisualFactoryCache::GetRenderer( const std::string& key ) const
98 int index = FindRenderer( key );
101 return mRenderers[ index ]->mRenderer.GetHandle();
109 void VisualFactoryCache::SaveRenderer( const std::string& key, Renderer& renderer )
111 int hash = Dali::CalculateHash( key );
112 const CachedRenderer* cachedRenderer = new CachedRenderer( key, renderer );
114 CachedRenderers::Iterator it = std::find( mRenderers.Begin(), mRenderers.End(), static_cast< CachedRenderer* >( NULL ) );
115 if( it != mRenderers.End() )
117 *it = cachedRenderer;
118 int index = it - mRenderers.Begin();
119 mRendererHashes[ index ] = hash;
123 mRendererHashes.PushBack( hash );
124 mRenderers.PushBack( cachedRenderer );
128 bool VisualFactoryCache::CleanRendererCache( const std::string& key )
130 int index = FindRenderer( key );
133 const CachedRenderer*& cachedRenderer = mRenderers[ index ];
134 if( !cachedRenderer->mRenderer.GetHandle() )
136 mRendererHashes[ index ] = Dali::INITIAL_HASH_VALUE;
138 delete cachedRenderer;
139 cachedRenderer = NULL;
146 void VisualFactoryCache::CacheDebugVisual( Renderer& renderer )
148 mDebugVisual = renderer;
151 Renderer VisualFactoryCache::GetDebugVisual()
156 Geometry VisualFactoryCache::CreateQuadGeometry()
158 const float halfWidth = 0.5f;
159 const float halfHeight = 0.5f;
160 struct QuadVertex { Vector2 position;};
161 QuadVertex quadVertexData[4] =
163 { Vector2(-halfWidth, -halfHeight) },
164 { Vector2(-halfWidth, halfHeight) },
165 { Vector2( halfWidth, -halfHeight) },
166 { Vector2( halfWidth, halfHeight) }
169 Property::Map quadVertexFormat;
170 quadVertexFormat["aPosition"] = Property::VECTOR2;
171 PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
172 quadVertices.SetData( quadVertexData, 4 );
174 // Create the geometry object
175 Geometry geometry = Geometry::New();
176 geometry.AddVertexBuffer( quadVertices );
177 geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
182 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
184 if( !mSvgRasterizeThread )
186 mSvgRasterizeThread = new SvgRasterizeThread( new EventThreadCallback( MakeCallback( this, &VisualFactoryCache::ApplyRasterizedSVGToSampler ) ) );
187 mSvgRasterizeThread->Start();
189 return mSvgRasterizeThread;
192 void VisualFactoryCache::ApplyRasterizedSVGToSampler()
194 while( RasterizingTaskPtr task = mSvgRasterizeThread->NextCompletedTask() )
196 task->GetSvgVisual()->ApplyRasterizedImage( task->GetPixelData() );
200 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
202 uint16_t gridWidth = gridSize.GetWidth();
203 uint16_t gridHeight = gridSize.GetHeight();
206 Vector< Vector2 > vertices;
207 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
209 for( int y = 0; y < gridHeight + 1; ++y )
211 for( int x = 0; x < gridWidth + 1; ++x )
213 vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight - 0.5f) );
218 Vector< unsigned short > indices;
219 indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
221 for( unsigned int row = 0u; row < gridHeight; ++row )
223 unsigned int rowStartIndex = row*(gridWidth+1u);
224 unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
226 if( row != 0u ) // degenerate index on non-first row
228 indices.PushBack( rowStartIndex );
231 for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
233 indices.PushBack( rowStartIndex + column);
234 indices.PushBack( nextRowStartIndex + column);
237 if( row != gridHeight-1u ) // degenerate index on non-last row
239 indices.PushBack( nextRowStartIndex + gridWidth );
243 Property::Map vertexFormat;
244 vertexFormat[ "aPosition" ] = Property::VECTOR2;
245 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
246 if( vertices.Size() > 0 )
248 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
251 Property::Map indexFormat;
252 indexFormat[ "indices" ] = Property::INTEGER;
253 PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat );
256 // Create the geometry object
257 Geometry geometry = Geometry::New();
258 geometry.AddVertexBuffer( vertexPropertyBuffer );
259 if( indices.Size() > 0 )
261 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
264 geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
269 } // namespace Internal
271 } // namespace Toolkit