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 ),
47 mPreMultiplyOnLoad( preMultiplyOnLoad )
51 VisualFactoryCache::~VisualFactoryCache()
53 SvgRasterizeThread::TerminateThread( mSvgRasterizeThread );
56 Geometry VisualFactoryCache::GetGeometry( GeometryType type )
58 if( !mGeometry[type] && type == QUAD_GEOMETRY )
60 mGeometry[type] = CreateQuadGeometry();
63 return mGeometry[type];
66 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
68 mGeometry[type] = geometry;
71 Shader VisualFactoryCache::GetShader( ShaderType type )
76 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
78 mShader[type] = shader;
81 Geometry VisualFactoryCache::CreateQuadGeometry()
83 const float halfWidth = 0.5f;
84 const float halfHeight = 0.5f;
85 struct QuadVertex { Vector2 position;};
86 QuadVertex quadVertexData[4] =
88 { Vector2(-halfWidth, -halfHeight) },
89 { Vector2(-halfWidth, halfHeight) },
90 { Vector2( halfWidth, -halfHeight) },
91 { Vector2( halfWidth, halfHeight) }
94 Property::Map quadVertexFormat;
95 quadVertexFormat["aPosition"] = Property::VECTOR2;
96 PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
97 quadVertices.SetData( quadVertexData, 4 );
99 // Create the geometry object
100 Geometry geometry = Geometry::New();
101 geometry.AddVertexBuffer( quadVertices );
102 geometry.SetType( Geometry::TRIANGLE_STRIP );
107 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
111 mAtlasManager = new ImageAtlasManager();
112 mAtlasManager->SetBrokenImage( mBrokenImageUrl );
115 return mAtlasManager;
118 TextureManager& VisualFactoryCache::GetTextureManager()
120 return mTextureManager;
123 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
125 return mNPatchLoader;
128 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
130 if( !mSvgRasterizeThread )
132 mSvgRasterizeThread = new SvgRasterizeThread( new EventThreadCallback( MakeCallback( this, &VisualFactoryCache::ApplyRasterizedSVGToSampler ) ) );
133 mSvgRasterizeThread->Start();
135 return mSvgRasterizeThread;
138 void VisualFactoryCache::ApplyRasterizedSVGToSampler()
140 while( RasterizingTaskPtr task = mSvgRasterizeThread->NextCompletedTask() )
142 task->GetSvgVisual()->ApplyRasterizedImage( task->GetPixelData() );
146 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
148 uint16_t gridWidth = gridSize.GetWidth();
149 uint16_t gridHeight = gridSize.GetHeight();
152 Vector< Vector2 > vertices;
153 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
155 for( int y = 0; y < gridHeight + 1; ++y )
157 for( int x = 0; x < gridWidth + 1; ++x )
159 vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight - 0.5f) );
164 Vector< unsigned short > indices;
165 indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
167 for( unsigned int row = 0u; row < gridHeight; ++row )
169 unsigned int rowStartIndex = row*(gridWidth+1u);
170 unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
172 if( row != 0u ) // degenerate index on non-first row
174 indices.PushBack( rowStartIndex );
177 for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
179 indices.PushBack( rowStartIndex + column);
180 indices.PushBack( nextRowStartIndex + column);
183 if( row != gridHeight-1u ) // degenerate index on non-last row
185 indices.PushBack( nextRowStartIndex + gridWidth );
189 Property::Map vertexFormat;
190 vertexFormat[ "aPosition" ] = Property::VECTOR2;
191 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
192 if( vertices.Size() > 0 )
194 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
197 Property::Map indexFormat;
198 indexFormat[ "indices" ] = Property::INTEGER;
199 PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat );
202 // Create the geometry object
203 Geometry geometry = Geometry::New();
204 geometry.AddVertexBuffer( vertexPropertyBuffer );
205 if( indices.Size() > 0 )
207 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
210 geometry.SetType( Geometry::TRIANGLE_STRIP );
215 Image VisualFactoryCache::GetBrokenVisualImage()
217 return ResourceImage::New( mBrokenImageUrl );
220 void VisualFactoryCache::SetPreMultiplyOnLoad( bool preMultiply )
222 mPreMultiplyOnLoad = preMultiply;
225 bool VisualFactoryCache::GetPreMultiplyOnLoad()
227 return mPreMultiplyOnLoad;
230 void VisualFactoryCache::SetBrokenImageUrl(const std::string& brokenImageUrl)
232 mBrokenImageUrl = brokenImageUrl;
236 mAtlasManager = new ImageAtlasManager();
239 mAtlasManager->SetBrokenImage( mBrokenImageUrl );
240 mTextureManager.SetBrokenImageUrl( mBrokenImageUrl );
243 } // namespace Internal
245 } // namespace Toolkit