2 * Copyright (c) 2024 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 <dali-toolkit/internal/visuals/visual-factory-cache.h>
21 #include <dali/devel-api/adaptor-framework/environment-variable.h>
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/common/hash.h>
24 #include <dali/devel-api/scripting/enum-helper.h>
25 #include <dali/devel-api/scripting/scripting.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/math/math-utils.h>
30 #include <dali-toolkit/devel-api/utility/npatch-helper.h>
31 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
32 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-manager.h>
33 #include <dali-toolkit/internal/visuals/color/color-visual.h>
34 #include <dali-toolkit/internal/visuals/image/image-atlas-manager.h>
35 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
36 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
46 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
48 constexpr auto LOAD_IMAGE_YUV_PLANES_ENV = "DALI_LOAD_IMAGE_YUV_PLANES";
50 bool NeedToLoadYuvPlanes()
52 auto loadYuvPlanesString = Dali::EnvironmentVariable::GetEnvironmentVariable(LOAD_IMAGE_YUV_PLANES_ENV);
53 bool loadYuvPlanes = loadYuvPlanesString ? std::atoi(loadYuvPlanesString) : false;
59 VisualFactoryCache::VisualFactoryCache(bool preMultiplyOnLoad)
60 : mLoadYuvPlanes(NeedToLoadYuvPlanes()),
61 mTextureManager(mLoadYuvPlanes),
62 mVectorAnimationManager(nullptr),
63 mPreMultiplyOnLoad(preMultiplyOnLoad),
64 mBrokenImageInfoContainer(),
65 mDefaultBrokenImageUrl(""),
66 mUseDefaultBrokenImageOnly(true)
68 mSvgLoader.SetVisualFactoryCache(*this);
71 VisualFactoryCache::~VisualFactoryCache()
75 Geometry VisualFactoryCache::GetGeometry(GeometryType type)
77 if(!mGeometry[type] && type == QUAD_GEOMETRY)
79 mGeometry[type] = CreateQuadGeometry();
82 return mGeometry[type];
85 void VisualFactoryCache::SaveGeometry(GeometryType type, Geometry geometry)
87 mGeometry[type] = geometry;
90 Shader VisualFactoryCache::GetShader(ShaderType type)
95 Shader VisualFactoryCache::GenerateAndSaveShader(ShaderType type, std::string_view vertexShader, std::string_view fragmentShader)
97 Shader shader = Shader::New(vertexShader, fragmentShader, Shader::Hint::NONE, Scripting::GetLinearEnumerationName<ShaderType>(type, VISUAL_SHADER_TYPE_TABLE, VISUAL_SHADER_TYPE_TABLE_COUNT));
98 mShader[type] = shader;
102 Geometry VisualFactoryCache::CreateQuadGeometry()
104 const float halfWidth = 0.5f;
105 const float halfHeight = 0.5f;
110 QuadVertex quadVertexData[4] =
112 {Vector2(-halfWidth, -halfHeight)},
113 {Vector2(-halfWidth, halfHeight)},
114 {Vector2(halfWidth, -halfHeight)},
115 {Vector2(halfWidth, halfHeight)}};
117 Property::Map quadVertexFormat;
118 quadVertexFormat["aPosition"] = Property::VECTOR2;
119 VertexBuffer quadVertices = VertexBuffer::New(quadVertexFormat);
120 quadVertices.SetData(quadVertexData, 4);
122 // Create the geometry object
123 Geometry geometry = Geometry::New();
124 geometry.AddVertexBuffer(quadVertices);
125 geometry.SetType(Geometry::TRIANGLE_STRIP);
130 ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager()
134 mAtlasManager = new ImageAtlasManager();
135 mAtlasManager->SetBrokenImage(mDefaultBrokenImageUrl);
138 return mAtlasManager;
141 TextureManager& VisualFactoryCache::GetTextureManager()
143 return mTextureManager;
146 NPatchLoader& VisualFactoryCache::GetNPatchLoader()
148 return mNPatchLoader;
151 SvgLoader& VisualFactoryCache::GetSvgLoader()
156 VectorAnimationManager& VisualFactoryCache::GetVectorAnimationManager()
158 if(!mVectorAnimationManager)
160 mVectorAnimationManager = std::unique_ptr<VectorAnimationManager>(new VectorAnimationManager());
162 return *mVectorAnimationManager;
165 void VisualFactoryCache::FinalizeVectorAnimationManager()
167 if(mVectorAnimationManager)
169 mVectorAnimationManager->Finalize();
173 Geometry VisualFactoryCache::CreateGridGeometry(Uint16Pair gridSize)
175 uint16_t gridWidth = gridSize.GetWidth();
176 uint16_t gridHeight = gridSize.GetHeight();
179 Vector<Vector2> vertices;
180 vertices.Reserve((gridWidth + 1) * (gridHeight + 1));
182 for(int y = 0; y < gridHeight + 1; ++y)
184 for(int x = 0; x < gridWidth + 1; ++x)
186 vertices.PushBack(Vector2((float)x / gridWidth - 0.5f, (float)y / gridHeight - 0.5f));
191 Vector<unsigned short> indices;
192 indices.Reserve((gridWidth + 2) * gridHeight * 2 - 2);
194 for(unsigned int row = 0u; row < gridHeight; ++row)
196 unsigned int rowStartIndex = row * (gridWidth + 1u);
197 unsigned int nextRowStartIndex = rowStartIndex + gridWidth + 1u;
199 if(row != 0u) // degenerate index on non-first row
201 indices.PushBack(rowStartIndex);
204 for(unsigned int column = 0u; column < gridWidth + 1u; column++) // main strip
206 indices.PushBack(rowStartIndex + column);
207 indices.PushBack(nextRowStartIndex + column);
210 if(row != gridHeight - 1u) // degenerate index on non-last row
212 indices.PushBack(nextRowStartIndex + gridWidth);
216 Property::Map vertexFormat;
217 vertexFormat["aPosition"] = Property::VECTOR2;
218 VertexBuffer vertexBuffer = VertexBuffer::New(vertexFormat);
219 if(vertices.Size() > 0)
221 vertexBuffer.SetData(&vertices[0], vertices.Size());
224 Property::Map indexFormat;
225 indexFormat["indices"] = Property::INTEGER;
226 VertexBuffer indexVertexBuffer = VertexBuffer::New(indexFormat);
228 // Create the geometry object
229 Geometry geometry = Geometry::New();
230 geometry.AddVertexBuffer(vertexBuffer);
231 if(indices.Size() > 0)
233 geometry.SetIndexBuffer(&indices[0], indices.Size());
236 geometry.SetType(Geometry::TRIANGLE_STRIP);
241 Texture VisualFactoryCache::GetBrokenVisualImage(uint32_t brokenIndex)
243 if(!(mBrokenImageInfoContainer[brokenIndex].texture))
246 Devel::PixelBuffer pixelBuffer = LoadImageFromFile(mBrokenImageInfoContainer[brokenIndex].url);
249 pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
250 mBrokenImageInfoContainer[brokenIndex].texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
251 mBrokenImageInfoContainer[brokenIndex].texture.Upload(pixelData);
252 mBrokenImageInfoContainer[brokenIndex].width = pixelData.GetWidth();
253 mBrokenImageInfoContainer[brokenIndex].height = pixelData.GetHeight();
256 return mBrokenImageInfoContainer[brokenIndex].texture;
259 void VisualFactoryCache::SetPreMultiplyOnLoad(bool preMultiply)
261 mPreMultiplyOnLoad = preMultiply;
264 bool VisualFactoryCache::GetPreMultiplyOnLoad() const
266 return mPreMultiplyOnLoad;
269 bool VisualFactoryCache::GetLoadYuvPlanes() const
271 return mLoadYuvPlanes;
274 void VisualFactoryCache::SetBrokenImageUrl(std::string& defaultBrokenUrl, const std::vector<std::string>& brokenImageUrlList)
276 mUseDefaultBrokenImageOnly = false;
277 mBrokenImageInfoContainer.clear();
278 mBrokenImageInfoContainer.assign(brokenImageUrlList.size(), BrokenImageInfo());
279 for(unsigned int i = 0; i < brokenImageUrlList.size(); i++)
281 mBrokenImageInfoContainer[i].url = brokenImageUrlList[i];
284 mDefaultBrokenImageUrl = defaultBrokenUrl;
287 VisualUrl::Type VisualFactoryCache::GetBrokenImageVisualType(int index)
289 return mBrokenImageInfoContainer[index].visualType;
292 Geometry VisualFactoryCache::GetNPatchGeometry(int index)
296 if(mNPatchLoader.GetNPatchData(mBrokenImageInfoContainer[index].npatchId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
298 if(data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1)
300 geometry = GetGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY);
303 geometry = NPatchHelper::CreateGridGeometry(Uint16Pair(3, 3));
304 SaveGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY, geometry);
307 else if(data->GetStretchPixelsX().Size() > 0 || data->GetStretchPixelsY().Size() > 0)
309 Uint16Pair gridSize(2 * data->GetStretchPixelsX().Size() + 1, 2 * data->GetStretchPixelsY().Size() + 1);
310 geometry = NPatchHelper::CreateGridGeometry(gridSize);
315 // no N patch data so use default geometry
316 geometry = GetGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY);
319 geometry = NPatchHelper::CreateGridGeometry(Uint16Pair(3, 3));
320 SaveGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY, geometry);
326 Shader VisualFactoryCache::GetNPatchShader(int index)
330 // 0 is either no data (load failed?) or no stretch regions on image
331 // for both cases we use the default shader
332 NPatchUtility::StretchRanges::SizeType xStretchCount = 0;
333 NPatchUtility::StretchRanges::SizeType yStretchCount = 0;
335 // ask loader for the regions
336 if(mNPatchLoader.GetNPatchData(mBrokenImageInfoContainer[index].npatchId, data))
338 xStretchCount = data->GetStretchPixelsX().Count();
339 yStretchCount = data->GetStretchPixelsY().Count();
342 if(DALI_LIKELY((xStretchCount == 0 && yStretchCount == 0) || (xStretchCount == 1 && yStretchCount == 1)))
344 shader = GetShader(VisualFactoryCache::NINE_PATCH_SHADER);
345 if(DALI_UNLIKELY(!shader))
347 // Only cache vanilla 9 patch shaders
348 shader = GenerateAndSaveShader(VisualFactoryCache::NINE_PATCH_SHADER, SHADER_NPATCH_VISUAL_3X3_SHADER_VERT, SHADER_NPATCH_VISUAL_SHADER_FRAG);
351 else if(xStretchCount > 0 || yStretchCount > 0)
353 std::stringstream shaderName;
354 shaderName << "N_PATCH_" << xStretchCount << "x" << yStretchCount;
356 std::stringstream vertexShader;
357 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
358 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
359 << SHADER_NPATCH_VISUAL_SHADER_VERT;
360 shader = Shader::New(vertexShader.str(), SHADER_NPATCH_VISUAL_SHADER_FRAG, Dali::Shader::Hint::NONE, shaderName.str());
365 void VisualFactoryCache::ApplyTextureAndUniforms(Renderer& renderer, int index)
368 TextureSet textureSet;
369 if(mNPatchLoader.GetNPatchData(mBrokenImageInfoContainer[index].npatchId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
371 textureSet = data->GetTextures();
372 mBrokenImageInfoContainer[index].texture = textureSet.GetTexture(0);
373 NPatchHelper::ApplyTextureAndUniforms(renderer, data.Get());
374 renderer.SetTextures(textureSet);
378 void VisualFactoryCache::UpdateBrokenImageRenderer(Renderer& renderer, const Vector2& size, const bool rendererIsImage)
380 bool useDefaultBrokenImage = false;
381 if(mBrokenImageInfoContainer.size() == 0)
383 useDefaultBrokenImage = true;
386 // Load Information for broken image
387 for(uint32_t index = 0; (index < mBrokenImageInfoContainer.size()) && !useDefaultBrokenImage; index++)
389 if(mBrokenImageInfoContainer[index].width == 0 && mBrokenImageInfoContainer[index].height == 0)
391 if(!mBrokenImageInfoContainer[index].url.empty())
393 VisualUrl visualUrl(mBrokenImageInfoContainer[index].url);
394 mBrokenImageInfoContainer[index].visualType = visualUrl.GetType();
395 if(mBrokenImageInfoContainer[index].visualType == VisualUrl::Type::N_PATCH)
399 mBrokenImageInfoContainer[index].npatchId = mNPatchLoader.Load(mTextureManager, NULL, mBrokenImageInfoContainer[index].url, border, mPreMultiplyOnLoad, true);
400 if(mNPatchLoader.GetNPatchData(mBrokenImageInfoContainer[index].npatchId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
402 mBrokenImageInfoContainer[index].width = data->GetCroppedWidth();
403 mBrokenImageInfoContainer[index].height = data->GetCroppedHeight();
407 DALI_LOG_ERROR("Can't update renderer for broken image. maybe image loading is failed [index:%d] [path:%s] \n", index, mBrokenImageInfoContainer[index].url.c_str());
408 useDefaultBrokenImage = true;
413 if(!GetBrokenVisualImage(index))
415 DALI_LOG_ERROR("Can't update renderer for broken image. maybe image loading is failed [index:%d] [path:%s] \n", index, mBrokenImageInfoContainer[index].url.c_str());
416 useDefaultBrokenImage = true;
423 if(!mUseDefaultBrokenImageOnly && useDefaultBrokenImage)
426 mBrokenImageInfoContainer.clear();
428 // assign for broken image
429 const int defaultBrokenIndex = 0;
430 mBrokenImageInfoContainer.assign(1, BrokenImageInfo());
431 mBrokenImageInfoContainer[defaultBrokenIndex].url = mDefaultBrokenImageUrl;
432 VisualUrl visualUrl(mBrokenImageInfoContainer[defaultBrokenIndex].url);
433 mBrokenImageInfoContainer[defaultBrokenIndex].visualType = visualUrl.GetType();
434 mUseDefaultBrokenImageOnly = true;
437 // Set Texutre to renderer
438 int brokenIndex = GetProperBrokenImageIndex(size);
439 if(GetBrokenImageVisualType(brokenIndex) == VisualUrl::N_PATCH)
441 // Set geometry and shader for npatch
442 Geometry geometry = GetNPatchGeometry(brokenIndex);
443 Shader shader = GetNPatchShader(brokenIndex);
444 renderer.SetGeometry(geometry);
445 renderer.SetShader(shader);
446 ApplyTextureAndUniforms(renderer, brokenIndex);
450 // Create single image renderer only if rederer is not use normal ImageShader. i.e. npatch visual.
453 Geometry geometry = GetGeometry(QUAD_GEOMETRY);
454 Shader shader = GetShader(IMAGE_SHADER);
457 shader = GenerateAndSaveShader(IMAGE_SHADER, Dali::Shader::GetVertexShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_FRAG.data());
458 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
460 renderer.SetGeometry(geometry);
461 renderer.SetShader(shader);
463 Texture brokenImage = GetBrokenVisualImage(brokenIndex);
464 TextureSet textureSet = TextureSet::New();
465 textureSet.SetTexture(0u, brokenImage);
466 renderer.SetTextures(textureSet);
470 int32_t VisualFactoryCache::GetProperBrokenImageIndex(const Vector2& size)
472 // Sets the default broken type
473 int32_t returnIndex = 0;
474 if(Dali::EqualsZero(size.width) || Dali::EqualsZero(size.height) || mUseDefaultBrokenImageOnly)
476 // To do : Need to add observer about size
480 // Find the proper value if we know the size of the image
481 for(int32_t index = static_cast<int32_t>(mBrokenImageInfoContainer.size()) - 1; index >= 0; index--)
483 // Skip if the value is not set
484 if(mBrokenImageInfoContainer[index].width == 0 || mBrokenImageInfoContainer[index].height == 0)
489 if(mBrokenImageInfoContainer[index].width < size.width && mBrokenImageInfoContainer[index].height < size.height)
499 } // namespace Internal
501 } // namespace Toolkit