DALi Version 2.0.45
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-factory-cache.cpp
1 /*
2  * Copyright (c) 2021 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/adaptor-framework/image-loading.h>
22 #include <dali/devel-api/common/hash.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-manager.h>
26 #include <dali-toolkit/internal/visuals/color/color-visual.h>
27 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
28 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
29
30 namespace Dali
31 {
32 namespace Toolkit
33 {
34 namespace Internal
35 {
36 VisualFactoryCache::VisualFactoryCache(bool preMultiplyOnLoad)
37 : mSvgRasterizeThread(NULL),
38   mVectorAnimationManager(),
39   mBrokenImageUrl(""),
40   mPreMultiplyOnLoad(preMultiplyOnLoad)
41 {
42 }
43
44 VisualFactoryCache::~VisualFactoryCache()
45 {
46   SvgRasterizeThread::TerminateThread(mSvgRasterizeThread);
47 }
48
49 Geometry VisualFactoryCache::GetGeometry(GeometryType type)
50 {
51   if(!mGeometry[type] && type == QUAD_GEOMETRY)
52   {
53     mGeometry[type] = CreateQuadGeometry();
54   }
55
56   return mGeometry[type];
57 }
58
59 void VisualFactoryCache::SaveGeometry(GeometryType type, Geometry geometry)
60 {
61   mGeometry[type] = geometry;
62 }
63
64 Shader VisualFactoryCache::GetShader(ShaderType type)
65 {
66   return mShader[type];
67 }
68
69 void VisualFactoryCache::SaveShader(ShaderType type, Shader shader)
70 {
71   mShader[type] = shader;
72 }
73
74 Geometry VisualFactoryCache::CreateQuadGeometry()
75 {
76   const float halfWidth  = 0.5f;
77   const float halfHeight = 0.5f;
78   struct QuadVertex
79   {
80     Vector2 position;
81   };
82   QuadVertex quadVertexData[4] =
83     {
84       {Vector2(-halfWidth, -halfHeight)},
85       {Vector2(-halfWidth, halfHeight)},
86       {Vector2(halfWidth, -halfHeight)},
87       {Vector2(halfWidth, halfHeight)}};
88
89   Property::Map quadVertexFormat;
90   quadVertexFormat["aPosition"] = Property::VECTOR2;
91   VertexBuffer quadVertices     = VertexBuffer::New(quadVertexFormat);
92   quadVertices.SetData(quadVertexData, 4);
93
94   // Create the geometry object
95   Geometry geometry = Geometry::New();
96   geometry.AddVertexBuffer(quadVertices);
97   geometry.SetType(Geometry::TRIANGLE_STRIP);
98
99   return geometry;
100 }
101
102 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
103 {
104   if(!mAtlasManager)
105   {
106     mAtlasManager = new ImageAtlasManager();
107     mAtlasManager->SetBrokenImage(mBrokenImageUrl);
108   }
109
110   return mAtlasManager;
111 }
112
113 TextureManager& VisualFactoryCache::GetTextureManager()
114 {
115   return mTextureManager;
116 }
117
118 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
119 {
120   return mNPatchLoader;
121 }
122
123 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
124 {
125   if(!mSvgRasterizeThread)
126   {
127     mSvgRasterizeThread = new SvgRasterizeThread();
128     mSvgRasterizeThread->Start();
129   }
130   return mSvgRasterizeThread;
131 }
132
133 VectorAnimationManager& VisualFactoryCache::GetVectorAnimationManager()
134 {
135   if(!mVectorAnimationManager)
136   {
137     mVectorAnimationManager = std::unique_ptr<VectorAnimationManager>(new VectorAnimationManager());
138   }
139   return *mVectorAnimationManager;
140 }
141
142 Geometry VisualFactoryCache::CreateGridGeometry(Uint16Pair gridSize)
143 {
144   uint16_t gridWidth  = gridSize.GetWidth();
145   uint16_t gridHeight = gridSize.GetHeight();
146
147   // Create vertices
148   Vector<Vector2> vertices;
149   vertices.Reserve((gridWidth + 1) * (gridHeight + 1));
150
151   for(int y = 0; y < gridHeight + 1; ++y)
152   {
153     for(int x = 0; x < gridWidth + 1; ++x)
154     {
155       vertices.PushBack(Vector2((float)x / gridWidth - 0.5f, (float)y / gridHeight - 0.5f));
156     }
157   }
158
159   // Create indices
160   Vector<unsigned short> indices;
161   indices.Reserve((gridWidth + 2) * gridHeight * 2 - 2);
162
163   for(unsigned int row = 0u; row < gridHeight; ++row)
164   {
165     unsigned int rowStartIndex     = row * (gridWidth + 1u);
166     unsigned int nextRowStartIndex = rowStartIndex + gridWidth + 1u;
167
168     if(row != 0u) // degenerate index on non-first row
169     {
170       indices.PushBack(rowStartIndex);
171     }
172
173     for(unsigned int column = 0u; column < gridWidth + 1u; column++) // main strip
174     {
175       indices.PushBack(rowStartIndex + column);
176       indices.PushBack(nextRowStartIndex + column);
177     }
178
179     if(row != gridHeight - 1u) // degenerate index on non-last row
180     {
181       indices.PushBack(nextRowStartIndex + gridWidth);
182     }
183   }
184
185   Property::Map vertexFormat;
186   vertexFormat["aPosition"] = Property::VECTOR2;
187   VertexBuffer vertexBuffer = VertexBuffer::New(vertexFormat);
188   if(vertices.Size() > 0)
189   {
190     vertexBuffer.SetData(&vertices[0], vertices.Size());
191   }
192
193   Property::Map indexFormat;
194   indexFormat["indices"]         = Property::INTEGER;
195   VertexBuffer indexVertexBuffer = VertexBuffer::New(indexFormat);
196
197   // Create the geometry object
198   Geometry geometry = Geometry::New();
199   geometry.AddVertexBuffer(vertexBuffer);
200   if(indices.Size() > 0)
201   {
202     geometry.SetIndexBuffer(&indices[0], indices.Size());
203   }
204
205   geometry.SetType(Geometry::TRIANGLE_STRIP);
206
207   return geometry;
208 }
209
210 Texture VisualFactoryCache::GetBrokenVisualImage()
211 {
212   if(!mBrokenImageTexture && mBrokenImageUrl.size())
213   {
214     PixelData          data;
215     Devel::PixelBuffer pixelBuffer = LoadImageFromFile(mBrokenImageUrl);
216     if(pixelBuffer)
217     {
218       data                = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
219       mBrokenImageTexture = Texture::New(Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(), data.GetWidth(), data.GetHeight());
220       mBrokenImageTexture.Upload(data);
221     }
222   }
223   return mBrokenImageTexture;
224 }
225
226 void VisualFactoryCache::SetPreMultiplyOnLoad(bool preMultiply)
227 {
228   mPreMultiplyOnLoad = preMultiply;
229 }
230
231 bool VisualFactoryCache::GetPreMultiplyOnLoad()
232 {
233   return mPreMultiplyOnLoad;
234 }
235
236 void VisualFactoryCache::SetBrokenImageUrl(const std::string& brokenImageUrl)
237 {
238   mBrokenImageUrl = brokenImageUrl;
239
240   if(!mAtlasManager)
241   {
242     mAtlasManager = new ImageAtlasManager();
243   }
244
245   mAtlasManager->SetBrokenImage(mBrokenImageUrl);
246 }
247
248 } // namespace Internal
249
250 } // namespace Toolkit
251
252 } // namespace Dali