2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "scene-graph-texture-set.h"
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>
28 namespace //Unnamed namespace
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()
33 static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::TextureSet> gTextureSetMemoryPool;
34 return gTextureSetMemoryPool;
44 TextureSet* TextureSet::New()
46 return new(GetTextureSetMemoryPool().AllocateRawThreadSafe()) TextureSet();
49 TextureSet::TextureSet()
56 TextureSet::~TextureSet()
60 void TextureSet::operator delete(void* ptr)
62 GetTextureSetMemoryPool().FreeThreadSafe(static_cast<TextureSet*>(ptr));
65 void TextureSet::SetSampler(uint32_t index, Render::Sampler* sampler)
67 const uint32_t samplerCount = static_cast<uint32_t>(mSamplers.Size());
68 if(samplerCount < index + 1)
70 SetSamplerCount(index + 1);
73 mSamplers[index] = sampler;
75 if(index < static_cast<uint32_t>(mTextures.Size()) && mTextures[index])
77 // Send a message to the RenderManagerReserveMessageSlot
78 using DerivedType = MessageValue1<RenderManager, Render::TextureKey>;
80 // Reserve some memory inside the render queue
81 uint32_t* slot = mRenderMessageDispatcher->ReserveMessageSlot(sizeof(DerivedType));
83 // Construct message in the render queue memory; note that delete should not be called on the return value
84 new(slot) DerivedType(&mRenderMessageDispatcher->GetRenderManager(), &RenderManager::SetTextureUpdated, mTextures[index]);
89 // Check wheter we need to pop back sampler
94 void TextureSet::SetTexture(uint32_t index, const Render::TextureKey& texture)
96 const uint32_t textureCount = static_cast<uint32_t>(mTextures.Size());
97 if(textureCount < index + 1)
99 SetTextureCount(index + 1);
102 mTextures[index] = texture;
105 mHasAlpha |= texture->HasAlphaChannel();
107 // Send a message to the RenderManagerReserveMessageSlot
108 using DerivedType = MessageValue1<RenderManager, Render::TextureKey>;
110 // Reserve some memory inside the render queue
111 uint32_t* slot = mRenderMessageDispatcher->ReserveMessageSlot(sizeof(DerivedType));
113 // Construct message in the render queue memory; note that delete should not be called on the return value
114 new(slot) DerivedType(&mRenderMessageDispatcher->GetRenderManager(), &RenderManager::SetTextureUpdated, texture);
118 // Check wheter we need to pop back textures
123 void TextureSet::SetTextureCount(uint32_t count)
125 const uint32_t textureCount = static_cast<uint32_t>(mTextures.Size());
127 if(textureCount != count)
129 mTextures.Resize(count, Render::TextureKey{});
133 void TextureSet::SetSamplerCount(uint32_t count)
135 const uint32_t samplerCount = static_cast<uint32_t>(mSamplers.Size());
137 if(samplerCount != count)
139 mSamplers.Resize(count, nullptr);
143 void TextureSet::TrimContainers()
145 uint32_t textureCount = static_cast<uint32_t>(mTextures.Size());
146 uint32_t samplerCount = static_cast<uint32_t>(mSamplers.Size());
148 while(textureCount > 0u)
150 if(mTextures[textureCount - 1u])
157 while(samplerCount > 0u)
159 if(mSamplers[samplerCount - 1u])
166 SetTextureCount(textureCount);
167 SetSamplerCount(samplerCount);
170 bool TextureSet::HasAlpha() const
175 void TextureSet::SetRenderMessageDispatcher(RenderMessageDispatcher* renderMessageDispatcher)
177 mRenderMessageDispatcher = renderMessageDispatcher;
180 uint32_t TextureSet::GetMemoryPoolCapacity()
182 return GetTextureSetMemoryPool().GetCapacity();
185 } // namespace SceneGraph
187 } // namespace Internal