2 * Copyright (c) 2020 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/devel-api/adaptor-framework/image-loading.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>
28 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-manager.h>
39 VisualFactoryCache::VisualFactoryCache( bool preMultiplyOnLoad )
40 : mSvgRasterizeThread( NULL ),
41 mVectorAnimationManager(),
43 mPreMultiplyOnLoad( preMultiplyOnLoad )
47 VisualFactoryCache::~VisualFactoryCache()
49 SvgRasterizeThread::TerminateThread( mSvgRasterizeThread );
52 Geometry VisualFactoryCache::GetGeometry( GeometryType type )
54 if( !mGeometry[type] && type == QUAD_GEOMETRY )
56 mGeometry[type] = CreateQuadGeometry();
59 return mGeometry[type];
62 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
64 mGeometry[type] = geometry;
67 Shader VisualFactoryCache::GetShader( ShaderType type )
72 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
74 mShader[type] = shader;
77 Geometry VisualFactoryCache::CreateQuadGeometry()
79 const float halfWidth = 0.5f;
80 const float halfHeight = 0.5f;
81 struct QuadVertex { Vector2 position;};
82 QuadVertex quadVertexData[4] =
84 { Vector2(-halfWidth, -halfHeight) },
85 { Vector2(-halfWidth, halfHeight) },
86 { Vector2( halfWidth, -halfHeight) },
87 { Vector2( halfWidth, halfHeight) }
90 Property::Map quadVertexFormat;
91 quadVertexFormat["aPosition"] = Property::VECTOR2;
92 VertexBuffer quadVertices = VertexBuffer::New( quadVertexFormat );
93 quadVertices.SetData( quadVertexData, 4 );
95 // Create the geometry object
96 Geometry geometry = Geometry::New();
97 geometry.AddVertexBuffer( quadVertices );
98 geometry.SetType( Geometry::TRIANGLE_STRIP );
103 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
107 mAtlasManager = new ImageAtlasManager();
108 mAtlasManager->SetBrokenImage( mBrokenImageUrl );
111 return mAtlasManager;
114 TextureManager& VisualFactoryCache::GetTextureManager()
116 return mTextureManager;
119 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
121 return mNPatchLoader;
124 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
126 if( !mSvgRasterizeThread )
128 mSvgRasterizeThread = new SvgRasterizeThread();
129 mSvgRasterizeThread->Start();
131 return mSvgRasterizeThread;
134 VectorAnimationManager& VisualFactoryCache::GetVectorAnimationManager()
136 if( !mVectorAnimationManager )
138 mVectorAnimationManager = std::unique_ptr< VectorAnimationManager >( new VectorAnimationManager() );
140 return *mVectorAnimationManager;
143 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
145 uint16_t gridWidth = gridSize.GetWidth();
146 uint16_t gridHeight = gridSize.GetHeight();
149 Vector< Vector2 > vertices;
150 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
152 for( int y = 0; y < gridHeight + 1; ++y )
154 for( int x = 0; x < gridWidth + 1; ++x )
156 vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight - 0.5f) );
161 Vector< unsigned short > indices;
162 indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
164 for( unsigned int row = 0u; row < gridHeight; ++row )
166 unsigned int rowStartIndex = row*(gridWidth+1u);
167 unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
169 if( row != 0u ) // degenerate index on non-first row
171 indices.PushBack( rowStartIndex );
174 for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
176 indices.PushBack( rowStartIndex + column);
177 indices.PushBack( nextRowStartIndex + column);
180 if( row != gridHeight-1u ) // degenerate index on non-last row
182 indices.PushBack( nextRowStartIndex + gridWidth );
186 Property::Map vertexFormat;
187 vertexFormat[ "aPosition" ] = Property::VECTOR2;
188 VertexBuffer vertexBuffer = VertexBuffer::New( vertexFormat );
189 if( vertices.Size() > 0 )
191 vertexBuffer.SetData( &vertices[ 0 ], vertices.Size() );
194 Property::Map indexFormat;
195 indexFormat[ "indices" ] = Property::INTEGER;
196 VertexBuffer indexVertexBuffer = VertexBuffer::New( indexFormat );
199 // Create the geometry object
200 Geometry geometry = Geometry::New();
201 geometry.AddVertexBuffer( vertexBuffer );
202 if( indices.Size() > 0 )
204 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
207 geometry.SetType( Geometry::TRIANGLE_STRIP );
212 Texture VisualFactoryCache::GetBrokenVisualImage()
214 if(!mBrokenImageTexture && mBrokenImageUrl.size())
217 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( mBrokenImageUrl );
220 data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
221 mBrokenImageTexture = Texture::New( Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(),
222 data.GetWidth(), data.GetHeight() );
223 mBrokenImageTexture.Upload( data );
226 return mBrokenImageTexture;
229 void VisualFactoryCache::SetPreMultiplyOnLoad( bool preMultiply )
231 mPreMultiplyOnLoad = preMultiply;
234 bool VisualFactoryCache::GetPreMultiplyOnLoad()
236 return mPreMultiplyOnLoad;
239 void VisualFactoryCache::SetBrokenImageUrl(const std::string& brokenImageUrl)
241 mBrokenImageUrl = brokenImageUrl;
245 mAtlasManager = new ImageAtlasManager();
248 mAtlasManager->SetBrokenImage( mBrokenImageUrl );
249 mTextureManager.SetBrokenImageUrl( mBrokenImageUrl );
252 } // namespace Internal
254 } // namespace Toolkit