[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / texture-set-impl.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
18 // CLASS HEADER
19 #include <dali/internal/event/rendering/texture-set-impl.h> // Dali::Internal::TextureSet
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/update/manager/update-manager.h>
23 #include <dali/internal/update/rendering/scene-graph-texture-set.h>
24 #include <dali/public-api/object/type-registry.h>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 TextureSetPtr TextureSet::New()
31 {
32   TextureSetPtr textureSet(new TextureSet());
33   textureSet->Initialize();
34   return textureSet;
35 }
36
37 void TextureSet::SetTexture(uint32_t index, TexturePtr texture)
38 {
39   uint32_t textureCount = static_cast<uint32_t>(mTextures.size());
40
41   if(textureCount < index + 1)
42   {
43     SetTextureCount(index + 1);
44   }
45
46   mTextures[index] = texture;
47
48   Render::TextureKey renderTexture{};
49   if(texture)
50   {
51     renderTexture = texture->GetRenderTextureKey();
52   }
53
54   SceneGraph::SetTextureMessage(mEventThreadServices, *mSceneObject, index, renderTexture);
55
56   if(!texture)
57   {
58     // Check wheter we need to pop back textures
59     TrimContainers();
60   }
61 }
62
63 Texture* TextureSet::GetTexture(uint32_t index) const
64 {
65   Texture* result(nullptr);
66   if(index < mTextures.size())
67   {
68     result = mTextures[index].Get();
69   }
70   else
71   {
72     DALI_LOG_ERROR("Error: Invalid index to TextureSet::GetTexture\n");
73   }
74
75   return result;
76 }
77
78 void TextureSet::SetSampler(uint32_t index, SamplerPtr sampler)
79 {
80   uint32_t samplerCount = static_cast<uint32_t>(mSamplers.size());
81   if(samplerCount < index + 1)
82   {
83     SetSamplerCount(index + 1);
84   }
85
86   mSamplers[index] = sampler;
87
88   Render::Sampler* renderSampler(nullptr);
89   if(sampler)
90   {
91     renderSampler = sampler->GetSamplerRenderObject();
92   }
93
94   SceneGraph::SetSamplerMessage(mEventThreadServices, *mSceneObject, index, renderSampler);
95
96   if(!sampler)
97   {
98     // Check wheter we need to pop back sampler
99     TrimContainers();
100   }
101 }
102
103 Sampler* TextureSet::GetSampler(uint32_t index) const
104 {
105   Sampler* result(nullptr);
106   if(index < mSamplers.size())
107   {
108     result = mSamplers[index].Get();
109   }
110   else
111   {
112     DALI_LOG_ERROR("Error: Invalid index to TextureSet::GetSampler\n");
113   }
114
115   return result;
116 }
117
118 uint32_t TextureSet::GetTextureCount() const
119 {
120   return static_cast<uint32_t>(mTextures.size());
121 }
122
123 const SceneGraph::TextureSet* TextureSet::GetTextureSetSceneObject() const
124 {
125   return mSceneObject;
126 }
127
128 void TextureSet::SetTextureCount(uint32_t count)
129 {
130   uint32_t textureCount = static_cast<uint32_t>(mTextures.size());
131   if(textureCount != count)
132   {
133     mTextures.resize(count, nullptr);
134   }
135 }
136
137 void TextureSet::SetSamplerCount(uint32_t count)
138 {
139   uint32_t samplerCount = static_cast<uint32_t>(mSamplers.size());
140   if(samplerCount != count)
141   {
142     mSamplers.resize(count, nullptr);
143   }
144 }
145
146 void TextureSet::TrimContainers()
147 {
148   uint32_t textureCount = static_cast<uint32_t>(mTextures.size());
149   uint32_t samplerCount = static_cast<uint32_t>(mSamplers.size());
150
151   while(textureCount > 0u)
152   {
153     if(mTextures[textureCount - 1u])
154     {
155       break;
156     }
157     --textureCount;
158   }
159
160   while(samplerCount > 0u)
161   {
162     if(mSamplers[samplerCount - 1u])
163     {
164       break;
165     }
166     --samplerCount;
167   }
168
169   SetTextureCount(textureCount);
170   SetSamplerCount(samplerCount);
171 }
172
173 TextureSet::TextureSet()
174 : mEventThreadServices(EventThreadServices::Get()),
175   mSceneObject(nullptr)
176 {
177 }
178
179 void TextureSet::Initialize()
180 {
181   SceneGraph::UpdateManager& updateManager = mEventThreadServices.GetUpdateManager();
182
183   mSceneObject = SceneGraph::TextureSet::New();
184   OwnerPointer<SceneGraph::TextureSet> transferOwnership(mSceneObject);
185   AddTextureSetMessage(updateManager, transferOwnership);
186 }
187
188 TextureSet::~TextureSet()
189 {
190   if(EventThreadServices::IsCoreRunning())
191   {
192     SceneGraph::UpdateManager& updateManager = mEventThreadServices.GetUpdateManager();
193     RemoveTextureSetMessage(updateManager, *mSceneObject);
194   }
195 }
196
197 } // namespace Internal
198 } // namespace Dali