2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "visual-factory-cache.h"
21 #include <dali/devel-api/adaptor-framework/image-loading.h>
22 #include <dali/devel-api/common/hash.h>
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>
36 VisualFactoryCache::VisualFactoryCache(bool preMultiplyOnLoad)
37 : mSvgRasterizeThread(NULL),
38 mVectorAnimationManager(),
40 mPreMultiplyOnLoad(preMultiplyOnLoad)
44 VisualFactoryCache::~VisualFactoryCache()
46 SvgRasterizeThread::TerminateThread(mSvgRasterizeThread);
49 Geometry VisualFactoryCache::GetGeometry(GeometryType type)
51 if(!mGeometry[type] && type == QUAD_GEOMETRY)
53 mGeometry[type] = CreateQuadGeometry();
56 return mGeometry[type];
59 void VisualFactoryCache::SaveGeometry(GeometryType type, Geometry geometry)
61 mGeometry[type] = geometry;
64 Shader VisualFactoryCache::GetShader(ShaderType type)
69 void VisualFactoryCache::SaveShader(ShaderType type, Shader shader)
71 mShader[type] = shader;
74 Geometry VisualFactoryCache::CreateQuadGeometry()
76 const float halfWidth = 0.5f;
77 const float halfHeight = 0.5f;
82 QuadVertex quadVertexData[4] =
84 {Vector2(-halfWidth, -halfHeight)},
85 {Vector2(-halfWidth, halfHeight)},
86 {Vector2(halfWidth, -halfHeight)},
87 {Vector2(halfWidth, halfHeight)}};
89 Property::Map quadVertexFormat;
90 quadVertexFormat["aPosition"] = Property::VECTOR2;
91 VertexBuffer quadVertices = VertexBuffer::New(quadVertexFormat);
92 quadVertices.SetData(quadVertexData, 4);
94 // Create the geometry object
95 Geometry geometry = Geometry::New();
96 geometry.AddVertexBuffer(quadVertices);
97 geometry.SetType(Geometry::TRIANGLE_STRIP);
102 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
106 mAtlasManager = new ImageAtlasManager();
107 mAtlasManager->SetBrokenImage(mBrokenImageUrl);
110 return mAtlasManager;
113 TextureManager& VisualFactoryCache::GetTextureManager()
115 return mTextureManager;
118 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
120 return mNPatchLoader;
123 SvgRasterizeThread* VisualFactoryCache::GetSVGRasterizationThread()
125 if(!mSvgRasterizeThread)
127 mSvgRasterizeThread = new SvgRasterizeThread();
128 mSvgRasterizeThread->Start();
130 return mSvgRasterizeThread;
133 VectorAnimationManager& VisualFactoryCache::GetVectorAnimationManager()
135 if(!mVectorAnimationManager)
137 mVectorAnimationManager = std::unique_ptr<VectorAnimationManager>(new VectorAnimationManager());
139 return *mVectorAnimationManager;
142 Geometry VisualFactoryCache::CreateGridGeometry(Uint16Pair gridSize)
144 uint16_t gridWidth = gridSize.GetWidth();
145 uint16_t gridHeight = gridSize.GetHeight();
148 Vector<Vector2> vertices;
149 vertices.Reserve((gridWidth + 1) * (gridHeight + 1));
151 for(int y = 0; y < gridHeight + 1; ++y)
153 for(int x = 0; x < gridWidth + 1; ++x)
155 vertices.PushBack(Vector2((float)x / gridWidth - 0.5f, (float)y / gridHeight - 0.5f));
160 Vector<unsigned short> indices;
161 indices.Reserve((gridWidth + 2) * gridHeight * 2 - 2);
163 for(unsigned int row = 0u; row < gridHeight; ++row)
165 unsigned int rowStartIndex = row * (gridWidth + 1u);
166 unsigned int nextRowStartIndex = rowStartIndex + gridWidth + 1u;
168 if(row != 0u) // degenerate index on non-first row
170 indices.PushBack(rowStartIndex);
173 for(unsigned int column = 0u; column < gridWidth + 1u; column++) // main strip
175 indices.PushBack(rowStartIndex + column);
176 indices.PushBack(nextRowStartIndex + column);
179 if(row != gridHeight - 1u) // degenerate index on non-last row
181 indices.PushBack(nextRowStartIndex + gridWidth);
185 Property::Map vertexFormat;
186 vertexFormat["aPosition"] = Property::VECTOR2;
187 VertexBuffer vertexBuffer = VertexBuffer::New(vertexFormat);
188 if(vertices.Size() > 0)
190 vertexBuffer.SetData(&vertices[0], vertices.Size());
193 Property::Map indexFormat;
194 indexFormat["indices"] = Property::INTEGER;
195 VertexBuffer indexVertexBuffer = VertexBuffer::New(indexFormat);
197 // Create the geometry object
198 Geometry geometry = Geometry::New();
199 geometry.AddVertexBuffer(vertexBuffer);
200 if(indices.Size() > 0)
202 geometry.SetIndexBuffer(&indices[0], indices.Size());
205 geometry.SetType(Geometry::TRIANGLE_STRIP);
210 Texture VisualFactoryCache::GetBrokenVisualImage()
212 if(!mBrokenImageTexture && mBrokenImageUrl.size())
215 Devel::PixelBuffer pixelBuffer = LoadImageFromFile(mBrokenImageUrl);
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);
223 return mBrokenImageTexture;
226 void VisualFactoryCache::SetPreMultiplyOnLoad(bool preMultiply)
228 mPreMultiplyOnLoad = preMultiply;
231 bool VisualFactoryCache::GetPreMultiplyOnLoad()
233 return mPreMultiplyOnLoad;
236 void VisualFactoryCache::SetBrokenImageUrl(const std::string& brokenImageUrl)
238 mBrokenImageUrl = brokenImageUrl;
242 mAtlasManager = new ImageAtlasManager();
245 mAtlasManager->SetBrokenImage(mBrokenImageUrl);
248 } // namespace Internal
250 } // namespace Toolkit