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