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()
44 : mSvgRasterizeThread( NULL )
48 VisualFactoryCache::~VisualFactoryCache()
50 SvgRasterizeThread::TerminateThread( mSvgRasterizeThread );
53 Geometry VisualFactoryCache::GetGeometry( GeometryType type )
55 if( !mGeometry[type] && type == QUAD_GEOMETRY )
57 mGeometry[type] = CreateQuadGeometry();
60 return mGeometry[type];
63 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
65 mGeometry[type] = geometry;
68 Shader VisualFactoryCache::GetShader( ShaderType type )
73 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
75 mShader[type] = shader;
78 Geometry VisualFactoryCache::CreateQuadGeometry()
80 const float halfWidth = 0.5f;
81 const float halfHeight = 0.5f;
82 struct QuadVertex { Vector2 position;};
83 QuadVertex quadVertexData[4] =
85 { Vector2(-halfWidth, -halfHeight) },
86 { Vector2(-halfWidth, halfHeight) },
87 { Vector2( halfWidth, -halfHeight) },
88 { Vector2( halfWidth, halfHeight) }
91 Property::Map quadVertexFormat;
92 quadVertexFormat["aPosition"] = Property::VECTOR2;
93 PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
94 quadVertices.SetData( quadVertexData, 4 );
96 // Create the geometry object
97 Geometry geometry = Geometry::New();
98 geometry.AddVertexBuffer( quadVertices );
99 geometry.SetType( Geometry::TRIANGLE_STRIP );
104 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
108 mAtlasManager = new ImageAtlasManager();
109 mAtlasManager->SetBrokenImage( BROKEN_VISUAL_IMAGE_URL );
112 return mAtlasManager;
115 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
117 return mNPatchLoader;
120 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
122 if( !mSvgRasterizeThread )
124 mSvgRasterizeThread = new SvgRasterizeThread( new EventThreadCallback( MakeCallback( this, &VisualFactoryCache::ApplyRasterizedSVGToSampler ) ) );
125 mSvgRasterizeThread->Start();
127 return mSvgRasterizeThread;
130 void VisualFactoryCache::ApplyRasterizedSVGToSampler()
132 while( RasterizingTaskPtr task = mSvgRasterizeThread->NextCompletedTask() )
134 task->GetSvgVisual()->ApplyRasterizedImage( task->GetPixelData() );
138 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
140 uint16_t gridWidth = gridSize.GetWidth();
141 uint16_t gridHeight = gridSize.GetHeight();
144 Vector< Vector2 > vertices;
145 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
147 for( int y = 0; y < gridHeight + 1; ++y )
149 for( int x = 0; x < gridWidth + 1; ++x )
151 vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight - 0.5f) );
156 Vector< unsigned short > indices;
157 indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
159 for( unsigned int row = 0u; row < gridHeight; ++row )
161 unsigned int rowStartIndex = row*(gridWidth+1u);
162 unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
164 if( row != 0u ) // degenerate index on non-first row
166 indices.PushBack( rowStartIndex );
169 for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
171 indices.PushBack( rowStartIndex + column);
172 indices.PushBack( nextRowStartIndex + column);
175 if( row != gridHeight-1u ) // degenerate index on non-last row
177 indices.PushBack( nextRowStartIndex + gridWidth );
181 Property::Map vertexFormat;
182 vertexFormat[ "aPosition" ] = Property::VECTOR2;
183 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
184 if( vertices.Size() > 0 )
186 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
189 Property::Map indexFormat;
190 indexFormat[ "indices" ] = Property::INTEGER;
191 PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat );
194 // Create the geometry object
195 Geometry geometry = Geometry::New();
196 geometry.AddVertexBuffer( vertexPropertyBuffer );
197 if( indices.Size() > 0 )
199 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
202 geometry.SetType( Geometry::TRIANGLE_STRIP );
207 Image VisualFactoryCache::GetBrokenVisualImage()
209 return ResourceImage::New( BROKEN_VISUAL_IMAGE_URL );
212 } // namespace Internal
214 } // namespace Toolkit