[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / update / rendering / scene-graph-texture-set.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_TEXTURE_SET_H
2 #define DALI_INTERNAL_SCENE_GRAPH_TEXTURE_SET_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // INTERNAL INCLUDES
21 #include <dali/public-api/rendering/texture-set.h>
22
23 #include <dali/internal/common/buffer-index.h>
24 #include <dali/internal/common/message.h>
25 #include <dali/internal/event/common/event-thread-services.h>
26 #include <dali/internal/render/renderers/render-texture-key.h>
27
28 namespace Dali
29 {
30 namespace Internal
31 {
32 namespace Render
33 {
34 struct Sampler;
35 class Texture;
36 } // namespace Render
37 namespace SceneGraph
38 {
39 class Renderer;
40 class RenderMessageDispatcher;
41
42 class TextureSet
43 {
44 public:
45   /**
46    * Construct a new TextureSet.
47    */
48   static TextureSet* New();
49
50   /**
51    * Destructor. Not virtual as not a base class and not inheriting anything
52    */
53   ~TextureSet();
54
55   /**
56    * Overriden delete operator
57    * Deletes the texture set from its global memory pool
58    */
59   void operator delete(void* ptr);
60
61   /**
62    * Set the sampler to be used by the texture at position "index"
63    * @param[in] index The index of the texture
64    * @param[in] sampler The sampler to be used by the texture
65    */
66   void SetSampler(uint32_t index, Render::Sampler* sampler);
67
68   /**
69    * Set the texture at position "index"
70    * @param[in] index The index of the texture
71    * @param[in] texture The texture
72    */
73   void SetTexture(uint32_t index, const Render::TextureKey& texture);
74
75   /**
76    * Return whether any texture in the texture set has an alpha channel
77    * @return true if at least one texture in the texture set has an alpha channel, false otherwise
78    */
79   bool HasAlpha() const;
80
81   /**
82    * Accessor for textures (used by RenderDataProvider impl)
83    */
84   const Vector<Render::TextureKey>& GetTextures()
85   {
86     return mTextures;
87   }
88
89   /**
90    * Accessor for samplers (used by RenderDataProvider impl)
91    */
92   const Vector<Render::Sampler*>& GetSamplers()
93   {
94     return mSamplers;
95   }
96
97   /**
98    * Set the renderMessageDispatcher to send message.
99    * @param[in] renderMessageDispatcher The renderMessageDispatcher to send messages.
100    */
101   void SetRenderMessageDispatcher(RenderMessageDispatcher* renderMessageDispatcher);
102
103   /**
104    * Get the capacity of the memory pools
105    * @return the capacity of the memory pools
106    */
107   static uint32_t GetMemoryPoolCapacity();
108
109 private:
110   /**
111    * Protected constructor; See also TextureSet::New()
112    */
113   TextureSet();
114
115 private:
116   /**
117    * @brief Change the count of texture set. It will increase count automatically if we need more textures.
118    *
119    * @param[in] count The number of textures what this texture set want to hold
120    */
121   void SetTextureCount(uint32_t count);
122
123   /**
124    * @brief Change the count of sampler set. It will increase count automatically if we need more samplers.
125    *
126    * @param[in] count The number of samplers what this texture set want to hold
127    */
128   void SetSamplerCount(uint32_t count);
129
130   /**
131    * @brief Remove empty textures and samplers at the back of container, and resize.
132    */
133   void TrimContainers();
134
135 private:
136   Vector<Render::Sampler*>   mSamplers;                         ///< List of samplers used by each texture. Not owned
137   Vector<Render::TextureKey> mTextures;                         ///< List of Textures. Not owned
138   RenderMessageDispatcher*   mRenderMessageDispatcher{nullptr}; ///< for sending messages to render thread. Not owned
139   bool                       mHasAlpha;                         ///< if any of the textures has an alpha channel
140 };
141
142 inline void SetTextureMessage(EventThreadServices& eventThreadServices, const TextureSet& textureSet, uint32_t index, const Render::TextureKey& textureKey)
143 {
144   using LocalType = MessageValue2<TextureSet, uint32_t, Render::TextureKey>;
145
146   // Reserve some memory inside the message queue
147   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
148
149   // Construct message in the message queue memory; note that delete should not be called on the return value
150   new(slot) LocalType(&textureSet, &TextureSet::SetTexture, index, textureKey);
151 }
152
153 inline void SetSamplerMessage(EventThreadServices& eventThreadServices, const TextureSet& textureSet, uint32_t index, Render::Sampler* sampler)
154 {
155   using LocalType = MessageValue2<TextureSet, uint32_t, Render::Sampler*>;
156
157   // Reserve some memory inside the message queue
158   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
159
160   // Construct message in the message queue memory; note that delete should not be called on the return value
161   new(slot) LocalType(&textureSet, &TextureSet::SetSampler, index, sampler);
162 }
163
164 } // namespace SceneGraph
165
166 } // namespace Internal
167
168 } // namespace Dali
169
170 #endif //  DALI_INTERNAL_SCENE_GRAPH_TEXTURE_SET_H