a229fe612bb09f2bc94f9ed34a5af842f7b263f8
[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
57 Texture* TextureSet::GetTexture(uint32_t index) const
58 {
59   Texture* result(nullptr);
60   if(index < mTextures.size())
61   {
62     result = mTextures[index].Get();
63   }
64   else
65   {
66     DALI_LOG_ERROR("Error: Invalid index to TextureSet::GetTexture\n");
67   }
68
69   return result;
70 }
71
72 void TextureSet::SetSampler(uint32_t index, SamplerPtr sampler)
73 {
74   uint32_t samplerCount = static_cast<uint32_t>(mSamplers.size());
75   if(samplerCount < index + 1)
76   {
77     SetSamplerCount(index + 1);
78   }
79
80   mSamplers[index] = sampler;
81
82   Render::Sampler* renderSampler(nullptr);
83   if(sampler)
84   {
85     renderSampler = sampler->GetSamplerRenderObject();
86   }
87
88   SceneGraph::SetSamplerMessage(mEventThreadServices, *mSceneObject, index, renderSampler);
89 }
90
91 Sampler* TextureSet::GetSampler(uint32_t index) const
92 {
93   Sampler* result(nullptr);
94   if(index < mSamplers.size())
95   {
96     result = mSamplers[index].Get();
97   }
98   else
99   {
100     DALI_LOG_ERROR("Error: Invalid index to TextureSet::GetSampler\n");
101   }
102
103   return result;
104 }
105
106 uint32_t TextureSet::GetTextureCount() const
107 {
108   return static_cast<uint32_t>(mTextures.size());
109 }
110
111 const SceneGraph::TextureSet* TextureSet::GetTextureSetSceneObject() const
112 {
113   return mSceneObject;
114 }
115
116 void TextureSet::SetTextureCount(uint32_t count)
117 {
118   uint32_t textureCount = static_cast<uint32_t>(mTextures.size());
119   if(textureCount != count)
120   {
121     mTextures.resize(count, nullptr);
122   }
123 }
124
125 void TextureSet::SetSamplerCount(uint32_t count)
126 {
127   uint32_t samplerCount = static_cast<uint32_t>(mSamplers.size());
128   if(samplerCount != count)
129   {
130     mSamplers.resize(count, nullptr);
131   }
132 }
133
134 void TextureSet::TrimContainers()
135 {
136   uint32_t textureCount = static_cast<uint32_t>(mTextures.size());
137   uint32_t samplerCount = static_cast<uint32_t>(mSamplers.size());
138
139   while(textureCount > 0u)
140   {
141     if(mTextures[textureCount - 1u])
142     {
143       break;
144     }
145     --textureCount;
146   }
147
148   while(samplerCount > 0u)
149   {
150     if(mSamplers[samplerCount - 1u])
151     {
152       break;
153     }
154     --samplerCount;
155   }
156
157   SetTextureCount(textureCount);
158   SetSamplerCount(samplerCount);
159 }
160
161 TextureSet::TextureSet()
162 : mEventThreadServices(EventThreadServices::Get()),
163   mSceneObject(nullptr)
164 {
165 }
166
167 void TextureSet::Initialize()
168 {
169   SceneGraph::UpdateManager& updateManager = mEventThreadServices.GetUpdateManager();
170
171   mSceneObject = SceneGraph::TextureSet::New();
172   OwnerPointer<SceneGraph::TextureSet> transferOwnership(mSceneObject);
173   AddTextureSetMessage(updateManager, transferOwnership);
174 }
175
176 TextureSet::~TextureSet()
177 {
178   if(EventThreadServices::IsCoreRunning())
179   {
180     SceneGraph::UpdateManager& updateManager = mEventThreadServices.GetUpdateManager();
181     RemoveTextureSetMessage(updateManager, *mSceneObject);
182   }
183 }
184
185 } // namespace Internal
186 } // namespace Dali