Merge "Add initialize resetter to reduce AnimatableProperty life" into devel/master
[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/common/render-manager.h>
24 #include <dali/internal/render/renderers/render-texture.h>
25 #include <dali/internal/update/controllers/render-message-dispatcher.h>
26 #include <dali/internal/update/rendering/scene-graph-renderer.h>
27
28 namespace //Unnamed namespace
29 {
30 //Memory pool used to allocate new texture sets. Memory used by this pool will be released when shutting down DALi
31 Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::TextureSet>& GetTextureSetMemoryPool()
32 {
33   static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::TextureSet> gTextureSetMemoryPool;
34   return gTextureSetMemoryPool;
35 }
36 } // namespace
37
38 namespace Dali
39 {
40 namespace Internal
41 {
42 namespace SceneGraph
43 {
44 TextureSet* TextureSet::New()
45 {
46   return new(GetTextureSetMemoryPool().AllocateRawThreadSafe()) TextureSet();
47 }
48
49 TextureSet::TextureSet()
50 : mSamplers(),
51   mHasAlpha(false)
52 {
53 }
54
55 TextureSet::~TextureSet()
56 {
57 }
58
59 void TextureSet::operator delete(void* ptr)
60 {
61   GetTextureSetMemoryPool().FreeThreadSafe(static_cast<TextureSet*>(ptr));
62 }
63
64 void TextureSet::SetSampler(uint32_t index, Render::Sampler* sampler)
65 {
66   const uint32_t samplerCount = static_cast<uint32_t>(mSamplers.Size());
67   if(samplerCount < index + 1)
68   {
69     mSamplers.Resize(index + 1);
70     for(uint32_t i(samplerCount); i <= index; ++i)
71     {
72       mSamplers[i] = nullptr;
73     }
74   }
75
76   mSamplers[index] = sampler;
77
78   if(index < static_cast<uint32_t>(mTextures.Size()))
79   {
80     // Send a message to the RenderManagerReserveMessageSlot
81     using DerivedType = MessageValue1<RenderManager, Render::TextureKey>;
82
83     // Reserve some memory inside the render queue
84     uint32_t* slot = mRenderMessageDispatcher->ReserveMessageSlot(sizeof(DerivedType));
85
86     // Construct message in the render queue memory; note that delete should not be called on the return value
87     new(slot) DerivedType(&mRenderMessageDispatcher->GetRenderManager(), &RenderManager::SetTextureUpdated, mTextures[index]);
88   }
89 }
90
91 void TextureSet::SetTexture(uint32_t index, const Render::TextureKey& texture)
92 {
93   const uint32_t textureCount = static_cast<uint32_t>(mTextures.Size());
94   if(textureCount < index + 1)
95   {
96     mTextures.Resize(index + 1);
97
98     bool samplerExist = true;
99     if(mSamplers.Size() < index + 1)
100     {
101       mSamplers.Resize(index + 1);
102       samplerExist = false;
103     }
104
105     for(uint32_t i(textureCount); i <= index; ++i)
106     {
107       mTextures[i] = Render::TextureKey{};
108
109       if(!samplerExist)
110       {
111         mSamplers[i] = nullptr;
112       }
113     }
114   }
115
116   mTextures[index] = texture;
117   if(texture)
118   {
119     mHasAlpha |= texture->HasAlphaChannel();
120
121     // Send a message to the RenderManagerReserveMessageSlot
122     using DerivedType = MessageValue1<RenderManager, Render::TextureKey>;
123
124     // Reserve some memory inside the render queue
125     uint32_t* slot = mRenderMessageDispatcher->ReserveMessageSlot(sizeof(DerivedType));
126
127     // Construct message in the render queue memory; note that delete should not be called on the return value
128     new(slot) DerivedType(&mRenderMessageDispatcher->GetRenderManager(), &RenderManager::SetTextureUpdated, texture);
129   }
130 }
131
132 bool TextureSet::HasAlpha() const
133 {
134   return mHasAlpha;
135 }
136
137 void TextureSet::SetRenderMessageDispatcher(RenderMessageDispatcher* renderMessageDispatcher)
138 {
139   mRenderMessageDispatcher = renderMessageDispatcher;
140 }
141
142 uint32_t TextureSet::GetMemoryPoolCapacity()
143 {
144   return GetTextureSetMemoryPool().GetCapacity();
145 }
146
147 } // namespace SceneGraph
148
149 } // namespace Internal
150
151 } // namespace Dali