[Tizen] Fixed actor relayout dimension dependencies
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / texture-set-impl.cpp
1 /*
2  * Copyright (c) 2021 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   if(index >= textureCount)
41   {
42     mTextures.resize(index + 1);
43
44     bool samplerExist = true;
45     if(mSamplers.size() < index + 1)
46     {
47       mSamplers.resize(index + 1);
48       samplerExist = false;
49     }
50
51     for(uint32_t i(textureCount); i <= index; ++i)
52     {
53       mTextures[i] = nullptr;
54
55       if(!samplerExist)
56       {
57         mSamplers[i] = nullptr;
58       }
59     }
60   }
61
62   mTextures[index] = texture;
63
64   Render::Texture* renderTexture(nullptr);
65   if(texture)
66   {
67     renderTexture = texture->GetRenderObject();
68   }
69
70   SceneGraph::SetTextureMessage(mEventThreadServices, *mSceneObject, index, renderTexture);
71 }
72
73 Texture* TextureSet::GetTexture(uint32_t index) const
74 {
75   Texture* result(nullptr);
76   if(index < mTextures.size())
77   {
78     result = mTextures[index].Get();
79   }
80   else
81   {
82     DALI_LOG_ERROR("Error: Invalid index to TextureSet::GetTexture\n");
83   }
84
85   return result;
86 }
87
88 void TextureSet::SetSampler(uint32_t index, SamplerPtr sampler)
89 {
90   uint32_t samplerCount = static_cast<uint32_t>(mSamplers.size());
91   if(samplerCount < index + 1)
92   {
93     mSamplers.resize(index + 1);
94     for(uint32_t i = samplerCount; i <= index; ++i)
95     {
96       mSamplers[i] = nullptr;
97     }
98   }
99
100   mSamplers[index] = sampler;
101
102   Render::Sampler* renderSampler(nullptr);
103   if(sampler)
104   {
105     renderSampler = sampler->GetSamplerRenderObject();
106   }
107
108   SceneGraph::SetSamplerMessage(mEventThreadServices, *mSceneObject, index, renderSampler);
109 }
110
111 Sampler* TextureSet::GetSampler(uint32_t index) const
112 {
113   Sampler* result(nullptr);
114   if(index < mSamplers.size())
115   {
116     result = mSamplers[index].Get();
117   }
118   else
119   {
120     DALI_LOG_ERROR("Error: Invalid index to TextureSet::GetSampler\n");
121   }
122
123   return result;
124 }
125
126 uint32_t TextureSet::GetTextureCount() const
127 {
128   return static_cast<uint32_t>(mTextures.size());
129 }
130
131 const SceneGraph::TextureSet* TextureSet::GetTextureSetSceneObject() const
132 {
133   return mSceneObject;
134 }
135
136 TextureSet::TextureSet()
137 : mEventThreadServices(EventThreadServices::Get()),
138   mSceneObject(nullptr)
139 {
140 }
141
142 void TextureSet::Initialize()
143 {
144   SceneGraph::UpdateManager& updateManager = mEventThreadServices.GetUpdateManager();
145
146   mSceneObject = SceneGraph::TextureSet::New();
147   OwnerPointer<SceneGraph::TextureSet> transferOwnership(mSceneObject);
148   AddTextureSetMessage(updateManager, transferOwnership);
149 }
150
151 TextureSet::~TextureSet()
152 {
153   if(EventThreadServices::IsCoreRunning())
154   {
155     SceneGraph::UpdateManager& updateManager = mEventThreadServices.GetUpdateManager();
156     RemoveTextureSetMessage(updateManager, *mSceneObject);
157   }
158 }
159
160 } // namespace Internal
161 } // namespace Dali