Merge "Fix resource leaks in layouting." into devel/master
[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   mBrokenImageUrl(""),
47   mPreMultiplyOnLoad( preMultiplyOnLoad )
48 {
49 }
50
51 VisualFactoryCache::~VisualFactoryCache()
52 {
53   SvgRasterizeThread::TerminateThread( mSvgRasterizeThread );
54 }
55
56 Geometry VisualFactoryCache::GetGeometry( GeometryType type )
57 {
58   if( !mGeometry[type] && type == QUAD_GEOMETRY )
59   {
60     mGeometry[type] = CreateQuadGeometry();
61   }
62
63   return mGeometry[type];
64 }
65
66 void VisualFactoryCache::SaveGeometry( GeometryType type, Geometry geometry )
67 {
68   mGeometry[type] = geometry;
69 }
70
71 Shader VisualFactoryCache::GetShader( ShaderType type )
72 {
73   return mShader[type];
74 }
75
76 void VisualFactoryCache::SaveShader( ShaderType type, Shader shader )
77 {
78   mShader[type] = shader;
79 }
80
81 Geometry VisualFactoryCache::CreateQuadGeometry()
82 {
83   const float halfWidth = 0.5f;
84   const float halfHeight = 0.5f;
85   struct QuadVertex { Vector2 position;};
86   QuadVertex quadVertexData[4] =
87   {
88       { Vector2(-halfWidth, -halfHeight) },
89       { Vector2(-halfWidth, halfHeight)  },
90       { Vector2( halfWidth, -halfHeight) },
91       { Vector2( halfWidth, halfHeight)  }
92   };
93
94   Property::Map quadVertexFormat;
95   quadVertexFormat["aPosition"] = Property::VECTOR2;
96   PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
97   quadVertices.SetData( quadVertexData, 4 );
98
99   // Create the geometry object
100   Geometry geometry = Geometry::New();
101   geometry.AddVertexBuffer( quadVertices );
102   geometry.SetType( Geometry::TRIANGLE_STRIP );
103
104   return geometry;
105 }
106
107 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
108 {
109   if( !mAtlasManager )
110   {
111     mAtlasManager = new ImageAtlasManager();
112     mAtlasManager->SetBrokenImage( mBrokenImageUrl );
113   }
114
115   return mAtlasManager;
116 }
117
118 TextureManager& VisualFactoryCache::GetTextureManager()
119 {
120   return mTextureManager;
121 }
122
123 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
124 {
125   return mNPatchLoader;
126 }
127
128 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
129 {
130   if( !mSvgRasterizeThread )
131   {
132     mSvgRasterizeThread = new SvgRasterizeThread( new EventThreadCallback( MakeCallback( this, &VisualFactoryCache::ApplyRasterizedSVGToSampler ) ) );
133     mSvgRasterizeThread->Start();
134   }
135   return mSvgRasterizeThread;
136 }
137
138 void VisualFactoryCache::ApplyRasterizedSVGToSampler()
139 {
140   while( RasterizingTaskPtr task = mSvgRasterizeThread->NextCompletedTask() )
141   {
142     task->GetSvgVisual()->ApplyRasterizedImage( task->GetPixelData() );
143   }
144 }
145
146 Geometry VisualFactoryCache::CreateGridGeometry( Uint16Pair gridSize )
147 {
148   uint16_t gridWidth = gridSize.GetWidth();
149   uint16_t gridHeight = gridSize.GetHeight();
150
151   // Create vertices
152   Vector< Vector2 > vertices;
153   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
154
155   for( int y = 0; y < gridHeight + 1; ++y )
156   {
157     for( int x = 0; x < gridWidth + 1; ++x )
158     {
159       vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight  - 0.5f) );
160     }
161   }
162
163   // Create indices
164   Vector< unsigned short > indices;
165   indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
166
167   for( unsigned int row = 0u; row < gridHeight; ++row )
168   {
169     unsigned int rowStartIndex = row*(gridWidth+1u);
170     unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
171
172     if( row != 0u ) // degenerate index on non-first row
173     {
174       indices.PushBack( rowStartIndex );
175     }
176
177     for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
178     {
179       indices.PushBack( rowStartIndex + column);
180       indices.PushBack( nextRowStartIndex + column);
181     }
182
183     if( row != gridHeight-1u ) // degenerate index on non-last row
184     {
185       indices.PushBack( nextRowStartIndex + gridWidth );
186     }
187   }
188
189   Property::Map vertexFormat;
190   vertexFormat[ "aPosition" ] = Property::VECTOR2;
191   PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
192   if( vertices.Size() > 0 )
193   {
194     vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
195   }
196
197   Property::Map indexFormat;
198   indexFormat[ "indices" ] = Property::INTEGER;
199   PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat );
200
201
202   // Create the geometry object
203   Geometry geometry = Geometry::New();
204   geometry.AddVertexBuffer( vertexPropertyBuffer );
205   if( indices.Size() > 0 )
206   {
207     geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
208   }
209
210   geometry.SetType( Geometry::TRIANGLE_STRIP );
211
212   return geometry;
213 }
214
215 Image VisualFactoryCache::GetBrokenVisualImage()
216 {
217   return ResourceImage::New( mBrokenImageUrl );
218 }
219
220 void VisualFactoryCache::SetPreMultiplyOnLoad( bool preMultiply )
221 {
222   mPreMultiplyOnLoad = preMultiply;
223 }
224
225 bool VisualFactoryCache::GetPreMultiplyOnLoad()
226 {
227   return mPreMultiplyOnLoad;
228 }
229
230 void VisualFactoryCache::SetBrokenImageUrl(const std::string& brokenImageUrl)
231 {
232   mBrokenImageUrl = brokenImageUrl;
233
234   if( !mAtlasManager )
235   {
236     mAtlasManager = new ImageAtlasManager();
237   }
238
239   mAtlasManager->SetBrokenImage( mBrokenImageUrl );
240   mTextureManager.SetBrokenImageUrl( mBrokenImageUrl );
241 }
242
243 } // namespace Internal
244
245 } // namespace Toolkit
246
247 } // namespace Dali