Make sure that global variables are initialized lazily.
[platform/core/uifw/dali-core.git] / dali / internal / update / rendering / scene-graph-texture-set.cpp
1 /*
2  * Copyright (c) 2023 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 "scene-graph-texture-set.h"
19
20 // INTERNAL HEADERS
21 #include <dali/internal/common/internal-constants.h>
22 #include <dali/internal/common/memory-pool-object-allocator.h>
23 #include <dali/internal/render/renderers/render-texture.h>
24 #include <dali/internal/update/rendering/scene-graph-renderer.h>
25
26 namespace //Unnamed namespace
27 {
28 //Memory pool used to allocate new texture sets. Memory used by this pool will be released when shutting down DALi
29 Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::TextureSet>& GetTextureSetMemoryPool()
30 {
31   static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::TextureSet> gTextureSetMemoryPool;
32   return gTextureSetMemoryPool;
33 }
34 } // namespace
35
36 namespace Dali
37 {
38 namespace Internal
39 {
40 namespace SceneGraph
41 {
42 TextureSet* TextureSet::New()
43 {
44   return new(GetTextureSetMemoryPool().AllocateRawThreadSafe()) TextureSet();
45 }
46
47 TextureSet::TextureSet()
48 : mSamplers(),
49   mHasAlpha(false)
50 {
51 }
52
53 TextureSet::~TextureSet()
54 {
55 }
56
57 void TextureSet::operator delete(void* ptr)
58 {
59   GetTextureSetMemoryPool().FreeThreadSafe(static_cast<TextureSet*>(ptr));
60 }
61
62 void TextureSet::SetSampler(uint32_t index, Render::Sampler* sampler)
63 {
64   const uint32_t samplerCount = static_cast<uint32_t>(mSamplers.Size());
65   if(samplerCount < index + 1)
66   {
67     mSamplers.Resize(index + 1);
68     for(uint32_t i(samplerCount); i <= index; ++i)
69     {
70       mSamplers[i] = nullptr;
71     }
72   }
73
74   mSamplers[index] = sampler;
75
76   if(index < static_cast<uint32_t>(mTextures.Size()))
77   {
78     mTextures[index]->SetUpdated(true);
79   }
80 }
81
82 void TextureSet::SetTexture(uint32_t index, const Render::TextureKey& texture)
83 {
84   const uint32_t textureCount = static_cast<uint32_t>(mTextures.Size());
85   if(textureCount < index + 1)
86   {
87     mTextures.Resize(index + 1);
88
89     bool samplerExist = true;
90     if(mSamplers.Size() < index + 1)
91     {
92       mSamplers.Resize(index + 1);
93       samplerExist = false;
94     }
95
96     for(uint32_t i(textureCount); i <= index; ++i)
97     {
98       mTextures[i] = Render::TextureKey{};
99
100       if(!samplerExist)
101       {
102         mSamplers[i] = nullptr;
103       }
104     }
105   }
106
107   mTextures[index] = texture;
108   if(texture)
109   {
110     mHasAlpha |= texture->HasAlphaChannel();
111     texture->SetUpdated(true);
112   }
113 }
114
115 bool TextureSet::HasAlpha() const
116 {
117   return mHasAlpha;
118 }
119
120 uint32_t TextureSet::GetMemoryPoolCapacity()
121 {
122   return GetTextureSetMemoryPool().GetCapacity();
123 }
124
125 } // namespace SceneGraph
126
127 } // namespace Internal
128
129 } // namespace Dali