2 * Copyright (c) 2016 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( bool preMultiplyOnLoad )
44 : mSvgRasterizeThread( NULL ),
45 mPreMultiplyOnLoad( preMultiplyOnLoad )
49 VisualFactoryCache::~VisualFactoryCache()
51 SvgRasterizeThread::TerminateThread( mSvgRasterizeThread );
54 Geometry VisualFactoryCache::GetGeometry( GeometryType type )
56 if( !mGeometry[type] && type == QUAD_GEOMETRY )
58 mGeometry[type] = CreateQuadGeometry();
61 return mGeometry[type];
64 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
66 mGeometry[type] = geometry;
69 Shader VisualFactoryCache::GetShader( ShaderType type )
74 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
76 mShader[type] = shader;
79 Geometry VisualFactoryCache::CreateQuadGeometry()
81 const float halfWidth = 0.5f;
82 const float halfHeight = 0.5f;
83 struct QuadVertex { Vector2 position;};
84 QuadVertex quadVertexData[4] =
86 { Vector2(-halfWidth, -halfHeight) },
87 { Vector2(-halfWidth, halfHeight) },
88 { Vector2( halfWidth, -halfHeight) },
89 { Vector2( halfWidth, halfHeight) }
92 Property::Map quadVertexFormat;
93 quadVertexFormat["aPosition"] = Property::VECTOR2;
94 PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
95 quadVertices.SetData( quadVertexData, 4 );
97 // Create the geometry object
98 Geometry geometry = Geometry::New();
99 geometry.AddVertexBuffer( quadVertices );
100 geometry.SetType( Geometry::TRIANGLE_STRIP );
105 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
109 mAtlasManager = new ImageAtlasManager();
110 mAtlasManager->SetBrokenImage( BROKEN_VISUAL_IMAGE_URL );
113 return mAtlasManager;
116 TextureManager& VisualFactoryCache::GetTextureManager()
118 return mTextureManager;
121 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
123 return mNPatchLoader;
126 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
128 if( !mSvgRasterizeThread )
130 mSvgRasterizeThread = new SvgRasterizeThread( new EventThreadCallback( MakeCallback( this, &VisualFactoryCache::ApplyRasterizedSVGToSampler ) ) );
131 mSvgRasterizeThread->Start();
133 return mSvgRasterizeThread;
136 void VisualFactoryCache::ApplyRasterizedSVGToSampler()
138 while( RasterizingTaskPtr task = mSvgRasterizeThread->NextCompletedTask() )
140 task->GetSvgVisual()->ApplyRasterizedImage( task->GetPixelData() );
144 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
146 uint16_t gridWidth = gridSize.GetWidth();
147 uint16_t gridHeight = gridSize.GetHeight();
150 Vector< Vector2 > vertices;
151 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
153 for( int y = 0; y < gridHeight + 1; ++y )
155 for( int x = 0; x < gridWidth + 1; ++x )
157 vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight - 0.5f) );
162 Vector< unsigned short > indices;
163 indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
165 for( unsigned int row = 0u; row < gridHeight; ++row )
167 unsigned int rowStartIndex = row*(gridWidth+1u);
168 unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
170 if( row != 0u ) // degenerate index on non-first row
172 indices.PushBack( rowStartIndex );
175 for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
177 indices.PushBack( rowStartIndex + column);
178 indices.PushBack( nextRowStartIndex + column);
181 if( row != gridHeight-1u ) // degenerate index on non-last row
183 indices.PushBack( nextRowStartIndex + gridWidth );
187 Property::Map vertexFormat;
188 vertexFormat[ "aPosition" ] = Property::VECTOR2;
189 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
190 if( vertices.Size() > 0 )
192 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
195 Property::Map indexFormat;
196 indexFormat[ "indices" ] = Property::INTEGER;
197 PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat );
200 // Create the geometry object
201 Geometry geometry = Geometry::New();
202 geometry.AddVertexBuffer( vertexPropertyBuffer );
203 if( indices.Size() > 0 )
205 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
208 geometry.SetType( Geometry::TRIANGLE_STRIP );
213 Image VisualFactoryCache::GetBrokenVisualImage()
215 return ResourceImage::New( BROKEN_VISUAL_IMAGE_URL );
218 void VisualFactoryCache::SetPreMultiplyOnLoad( bool preMultiply )
220 mPreMultiplyOnLoad = preMultiply;
223 bool VisualFactoryCache::GetPreMultiplyOnLoad()
225 return mPreMultiplyOnLoad;
228 } // namespace Internal
230 } // namespace Toolkit