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>
32 const char * const BROKEN_VISUAL_IMAGE_URL( DALI_IMAGE_DIR "broken.png");
44 VisualFactoryCache::VisualFactoryCache( bool preMultiplyOnLoad )
45 : mSvgRasterizeThread( NULL ),
46 mVectorAnimationThread(),
48 mPreMultiplyOnLoad( preMultiplyOnLoad )
52 VisualFactoryCache::~VisualFactoryCache()
54 SvgRasterizeThread::TerminateThread( mSvgRasterizeThread );
57 Geometry VisualFactoryCache::GetGeometry( GeometryType type )
59 if( !mGeometry[type] && type == QUAD_GEOMETRY )
61 mGeometry[type] = CreateQuadGeometry();
64 return mGeometry[type];
67 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
69 mGeometry[type] = geometry;
72 Shader VisualFactoryCache::GetShader( ShaderType type )
77 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
79 mShader[type] = shader;
82 Geometry VisualFactoryCache::CreateQuadGeometry()
84 const float halfWidth = 0.5f;
85 const float halfHeight = 0.5f;
86 struct QuadVertex { Vector2 position;};
87 QuadVertex quadVertexData[4] =
89 { Vector2(-halfWidth, -halfHeight) },
90 { Vector2(-halfWidth, halfHeight) },
91 { Vector2( halfWidth, -halfHeight) },
92 { Vector2( halfWidth, halfHeight) }
95 Property::Map quadVertexFormat;
96 quadVertexFormat["aPosition"] = Property::VECTOR2;
97 PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
98 quadVertices.SetData( quadVertexData, 4 );
100 // Create the geometry object
101 Geometry geometry = Geometry::New();
102 geometry.AddVertexBuffer( quadVertices );
103 geometry.SetType( Geometry::TRIANGLE_STRIP );
108 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
112 mAtlasManager = new ImageAtlasManager();
113 mAtlasManager->SetBrokenImage( mBrokenImageUrl );
116 return mAtlasManager;
119 TextureManager& VisualFactoryCache::GetTextureManager()
121 return mTextureManager;
124 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
126 return mNPatchLoader;
129 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
131 if( !mSvgRasterizeThread )
133 mSvgRasterizeThread = new SvgRasterizeThread( new EventThreadCallback( MakeCallback( this, &VisualFactoryCache::ApplyRasterizedSVGToSampler ) ) );
134 mSvgRasterizeThread->Start();
136 return mSvgRasterizeThread;
139 VectorAnimationThread& VisualFactoryCache::GetVectorAnimationThread()
141 if( !mVectorAnimationThread )
143 mVectorAnimationThread = std::unique_ptr< VectorAnimationThread >( new VectorAnimationThread() );
144 mVectorAnimationThread->Start();
146 return *mVectorAnimationThread;
149 void VisualFactoryCache::ApplyRasterizedSVGToSampler()
151 while( RasterizingTaskPtr task = mSvgRasterizeThread->NextCompletedTask() )
153 task->GetSvgVisual()->ApplyRasterizedImage( task->GetParsedImage(), task->GetPixelData() );
157 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
159 uint16_t gridWidth = gridSize.GetWidth();
160 uint16_t gridHeight = gridSize.GetHeight();
163 Vector< Vector2 > vertices;
164 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
166 for( int y = 0; y < gridHeight + 1; ++y )
168 for( int x = 0; x < gridWidth + 1; ++x )
170 vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight - 0.5f) );
175 Vector< unsigned short > indices;
176 indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
178 for( unsigned int row = 0u; row < gridHeight; ++row )
180 unsigned int rowStartIndex = row*(gridWidth+1u);
181 unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
183 if( row != 0u ) // degenerate index on non-first row
185 indices.PushBack( rowStartIndex );
188 for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
190 indices.PushBack( rowStartIndex + column);
191 indices.PushBack( nextRowStartIndex + column);
194 if( row != gridHeight-1u ) // degenerate index on non-last row
196 indices.PushBack( nextRowStartIndex + gridWidth );
200 Property::Map vertexFormat;
201 vertexFormat[ "aPosition" ] = Property::VECTOR2;
202 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
203 if( vertices.Size() > 0 )
205 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
208 Property::Map indexFormat;
209 indexFormat[ "indices" ] = Property::INTEGER;
210 PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat );
213 // Create the geometry object
214 Geometry geometry = Geometry::New();
215 geometry.AddVertexBuffer( vertexPropertyBuffer );
216 if( indices.Size() > 0 )
218 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
221 geometry.SetType( Geometry::TRIANGLE_STRIP );
226 Image VisualFactoryCache::GetBrokenVisualImage()
228 return ResourceImage::New( mBrokenImageUrl );
231 void VisualFactoryCache::SetPreMultiplyOnLoad( bool preMultiply )
233 mPreMultiplyOnLoad = preMultiply;
236 bool VisualFactoryCache::GetPreMultiplyOnLoad()
238 return mPreMultiplyOnLoad;
241 void VisualFactoryCache::SetBrokenImageUrl(const std::string& brokenImageUrl)
243 mBrokenImageUrl = brokenImageUrl;
247 mAtlasManager = new ImageAtlasManager();
250 mAtlasManager->SetBrokenImage( mBrokenImageUrl );
251 mTextureManager.SetBrokenImageUrl( mBrokenImageUrl );
254 } // namespace Internal
256 } // namespace Toolkit