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