876b1da9186186429a298413fcfb098e586cfe49
[platform/core/uifw/dali-core.git] / dali / internal / update / rendering / scene-graph-texture-set.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 // 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> gTextureSetMemoryPool;
30 } // namespace
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace SceneGraph
37 {
38 TextureSet* TextureSet::New()
39 {
40   return new(gTextureSetMemoryPool.AllocateRawThreadSafe()) TextureSet();
41 }
42
43 TextureSet::TextureSet()
44 : mSamplers(),
45   mRenderers(),
46   mHasAlpha(false)
47 {
48 }
49
50 TextureSet::~TextureSet()
51 {
52   for(auto&& renderer : mRenderers)
53   {
54     renderer->TextureSetDeleted();
55   }
56 }
57
58 void TextureSet::operator delete(void* ptr)
59 {
60   gTextureSetMemoryPool.FreeThreadSafe(static_cast<TextureSet*>(ptr));
61 }
62
63 void TextureSet::SetSampler(uint32_t index, Render::Sampler* sampler)
64 {
65   const uint32_t samplerCount = static_cast<uint32_t>(mSamplers.Size());
66   if(samplerCount < index + 1)
67   {
68     mSamplers.Resize(index + 1);
69     for(uint32_t i(samplerCount); i <= index; ++i)
70     {
71       mSamplers[i] = nullptr;
72     }
73   }
74
75   mSamplers[index] = sampler;
76   NotifyChangeToRenderers();
77 }
78
79 void TextureSet::SetTexture(uint32_t index, Render::Texture* texture)
80 {
81   const uint32_t textureCount = static_cast<uint32_t>(mTextures.Size());
82   if(textureCount < index + 1)
83   {
84     mTextures.Resize(index + 1);
85
86     bool samplerExist = true;
87     if(mSamplers.Size() < index + 1)
88     {
89       mSamplers.Resize(index + 1);
90       samplerExist = false;
91     }
92
93     for(uint32_t i(textureCount); i <= index; ++i)
94     {
95       mTextures[i] = nullptr;
96
97       if(!samplerExist)
98       {
99         mSamplers[i] = nullptr;
100       }
101     }
102   }
103
104   mTextures[index] = texture;
105   if(texture)
106   {
107     mHasAlpha |= texture->HasAlphaChannel();
108   }
109
110   NotifyChangeToRenderers();
111 }
112
113 bool TextureSet::HasAlpha() const
114 {
115   return mHasAlpha;
116 }
117
118 void TextureSet::AddObserver(Renderer* renderer)
119 {
120   for(auto&& element : mRenderers)
121   {
122     if(element == renderer)
123     {
124       //Renderer already in the list
125       return;
126     }
127   }
128
129   mRenderers.PushBack(renderer);
130 }
131
132 void TextureSet::RemoveObserver(Renderer* renderer)
133 {
134   for(auto &&iter = mRenderers.Begin(), end = mRenderers.End(); iter != end; ++iter)
135   {
136     if(*iter == renderer)
137     {
138       mRenderers.Remove(iter);
139       return;
140     }
141   }
142 }
143
144 void TextureSet::NotifyChangeToRenderers()
145 {
146   for(auto&& element : mRenderers)
147   {
148     element->TextureSetChanged();
149   }
150 }
151
152 } // namespace SceneGraph
153
154 } // namespace Internal
155
156 } // namespace Dali