Support the blur radius of the ColorVisual
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-factory-cache.cpp
1  /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // CLASS HEADER
18 #include "visual-factory-cache.h"
19
20 // EXTERNAL INCLUDES
21 #include <dali/devel-api/common/hash.h>
22 #include <dali/public-api/images/resource-image.h>
23
24 // INTERNAL INCLUDES
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
29
30 namespace
31 {
32 const char * const BROKEN_VISUAL_IMAGE_URL( DALI_IMAGE_DIR "broken.png");
33 }
34
35 namespace Dali
36 {
37
38 namespace Toolkit
39 {
40
41 namespace Internal
42 {
43
44 VisualFactoryCache::VisualFactoryCache( bool preMultiplyOnLoad )
45 : mSvgRasterizeThread( NULL ),
46   mVectorAnimationThread(),
47   mBrokenImageUrl(""),
48   mPreMultiplyOnLoad( preMultiplyOnLoad )
49 {
50 }
51
52 VisualFactoryCache::~VisualFactoryCache()
53 {
54   SvgRasterizeThread::TerminateThread( mSvgRasterizeThread );
55 }
56
57 Geometry VisualFactoryCache::GetGeometry( GeometryType type )
58 {
59   if( !mGeometry[type] && type == QUAD_GEOMETRY )
60   {
61     mGeometry[type] = CreateQuadGeometry();
62   }
63
64   return mGeometry[type];
65 }
66
67 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
68 {
69   mGeometry[type] = geometry;
70 }
71
72 Shader VisualFactoryCache::GetShader( ShaderType type )
73 {
74   return mShader[type];
75 }
76
77 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
78 {
79   mShader[type] = shader;
80 }
81
82 Geometry VisualFactoryCache::CreateQuadGeometry()
83 {
84   const float halfWidth = 0.5f;
85   const float halfHeight = 0.5f;
86   struct QuadVertex { Vector2 position;};
87   QuadVertex quadVertexData[4] =
88   {
89       { Vector2(-halfWidth, -halfHeight) },
90       { Vector2(-halfWidth, halfHeight)  },
91       { Vector2( halfWidth, -halfHeight) },
92       { Vector2( halfWidth, halfHeight)  }
93   };
94
95   Property::Map quadVertexFormat;
96   quadVertexFormat["aPosition"] = Property::VECTOR2;
97   PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
98   quadVertices.SetData( quadVertexData, 4 );
99
100   // Create the geometry object
101   Geometry geometry = Geometry::New();
102   geometry.AddVertexBuffer( quadVertices );
103   geometry.SetType( Geometry::TRIANGLE_STRIP );
104
105   return geometry;
106 }
107
108 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
109 {
110   if( !mAtlasManager )
111   {
112     mAtlasManager = new ImageAtlasManager();
113     mAtlasManager->SetBrokenImage( mBrokenImageUrl );
114   }
115
116   return mAtlasManager;
117 }
118
119 TextureManager& VisualFactoryCache::GetTextureManager()
120 {
121   return mTextureManager;
122 }
123
124 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
125 {
126   return mNPatchLoader;
127 }
128
129 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
130 {
131   if( !mSvgRasterizeThread )
132   {
133     mSvgRasterizeThread = new SvgRasterizeThread( new EventThreadCallback( MakeCallback( this, &VisualFactoryCache::ApplyRasterizedSVGToSampler ) ) );
134     mSvgRasterizeThread->Start();
135   }
136   return mSvgRasterizeThread;
137 }
138
139 VectorAnimationThread& VisualFactoryCache::GetVectorAnimationThread()
140 {
141   if( !mVectorAnimationThread )
142   {
143     mVectorAnimationThread = std::unique_ptr< VectorAnimationThread >( new VectorAnimationThread() );
144     mVectorAnimationThread->Start();
145   }
146   return *mVectorAnimationThread;
147 }
148
149 void VisualFactoryCache::ApplyRasterizedSVGToSampler()
150 {
151   while( RasterizingTaskPtr task = mSvgRasterizeThread->NextCompletedTask() )
152   {
153     task->GetSvgVisual()->ApplyRasterizedImage( task->GetParsedImage(), task->GetPixelData() );
154   }
155 }
156
157 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
158 {
159   uint16_t gridWidth = gridSize.GetWidth();
160   uint16_t gridHeight = gridSize.GetHeight();
161
162   // Create vertices
163   Vector< Vector2 > vertices;
164   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
165
166   for( int y = 0; y < gridHeight + 1; ++y )
167   {
168     for( int x = 0; x < gridWidth + 1; ++x )
169     {
170       vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight  - 0.5f) );
171     }
172   }
173
174   // Create indices
175   Vector< unsigned short > indices;
176   indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
177
178   for( unsigned int row = 0u; row < gridHeight; ++row )
179   {
180     unsigned int rowStartIndex = row*(gridWidth+1u);
181     unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
182
183     if( row != 0u ) // degenerate index on non-first row
184     {
185       indices.PushBack( rowStartIndex );
186     }
187
188     for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
189     {
190       indices.PushBack( rowStartIndex + column);
191       indices.PushBack( nextRowStartIndex + column);
192     }
193
194     if( row != gridHeight-1u ) // degenerate index on non-last row
195     {
196       indices.PushBack( nextRowStartIndex + gridWidth );
197     }
198   }
199
200   Property::Map vertexFormat;
201   vertexFormat[ "aPosition" ] = Property::VECTOR2;
202   PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
203   if( vertices.Size() > 0 )
204   {
205     vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
206   }
207
208   Property::Map indexFormat;
209   indexFormat[ "indices" ] = Property::INTEGER;
210   PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat );
211
212
213   // Create the geometry object
214   Geometry geometry = Geometry::New();
215   geometry.AddVertexBuffer( vertexPropertyBuffer );
216   if( indices.Size() > 0 )
217   {
218     geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
219   }
220
221   geometry.SetType( Geometry::TRIANGLE_STRIP );
222
223   return geometry;
224 }
225
226 Image VisualFactoryCache::GetBrokenVisualImage()
227 {
228   return ResourceImage::New( mBrokenImageUrl );
229 }
230
231 void VisualFactoryCache::SetPreMultiplyOnLoad( bool preMultiply )
232 {
233   mPreMultiplyOnLoad = preMultiply;
234 }
235
236 bool VisualFactoryCache::GetPreMultiplyOnLoad()
237 {
238   return mPreMultiplyOnLoad;
239 }
240
241 void VisualFactoryCache::SetBrokenImageUrl(const std::string& brokenImageUrl)
242 {
243   mBrokenImageUrl = brokenImageUrl;
244
245   if( !mAtlasManager )
246   {
247     mAtlasManager = new ImageAtlasManager();
248   }
249
250   mAtlasManager->SetBrokenImage( mBrokenImageUrl );
251   mTextureManager.SetBrokenImageUrl( mBrokenImageUrl );
252 }
253
254 } // namespace Internal
255
256 } // namespace Toolkit
257
258 } // namespace Dali