38ff4bb0a88e98c111660c91f63c3516281200b3
[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) 2021 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/internal/common/buffer-index.h>
22 #include <dali/internal/common/message.h>
23 #include <dali/internal/event/common/event-thread-services.h>
24 #include <dali/public-api/rendering/texture-set.h>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 namespace Render
31 {
32 struct Sampler;
33 class Texture;
34 } // namespace Render
35 namespace SceneGraph
36 {
37 class Renderer;
38
39 class TextureSet
40 {
41 public:
42   /**
43    * Construct a new TextureSet.
44    */
45   static TextureSet* New();
46
47   /**
48    * Destructor. Not virtual as not a base class and not inheriting anything
49    */
50   ~TextureSet();
51
52   /**
53    * Overriden delete operator
54    * Deletes the texture set from its global memory pool
55    */
56   void operator delete(void* ptr);
57
58   /**
59    * Set the sampler to be used by the texture at position "index"
60    * @param[in] index The index of the texture
61    * @param[in] sampler The sampler to be used by the texture
62    */
63   void SetSampler(uint32_t index, Render::Sampler* sampler);
64
65   /**
66    * Set the texture at position "index"
67    * @param[in] index The index of the texture
68    * @param[in] texture The texture
69    */
70   void SetTexture(uint32_t index, Render::Texture* texture);
71
72   /**
73    * Return whether any texture in the texture set has an alpha channel
74    * @return true if at least one texture in the texture set has an alpha channel, false otherwise
75    */
76   bool HasAlpha() const;
77
78   /**
79    * Adds a renderer to the Renderers list of the texture set.
80    * Renderers using the TextureSet get a notification when the texture set changes
81    *
82    * @param[in] renderer The renderer using the TextureSet
83    */
84   void AddObserver(Renderer* renderer);
85
86   /**
87    * Removes a renderer from the TextureSet renderers list
88    *
89    * @param[in] renderer The renderer no longer using the TextureSet
90    */
91   void RemoveObserver(Renderer* renderer);
92
93   /**
94    * Get the sampler of a texture in the TextureSet
95    * @param[in] index The index of the texture in the textures array
96    * @return the sampler used by the texture
97    */
98   Render::Sampler* GetTextureSampler(uint32_t index)
99   {
100     return mSamplers[index];
101   }
102
103   /**
104    * Get the number of Textures in the texture set
105    * @return The number of Textures
106    */
107   uint32_t GetTextureCount()
108   {
109     return static_cast<uint32_t>(mTextures.Size());
110   }
111
112   /**
113    * Get the pointer to  a Texture in the TextureSet
114    * @param[in] index The index of the texture in the textures array
115    * @return the pointer to the Texture in that position
116    */
117   Render::Texture* GetTexture(uint32_t index)
118   {
119     return mTextures[index];
120   }
121
122 private:
123   /**
124    * Protected constructor; See also TextureSet::New()
125    */
126   TextureSet();
127
128   /**
129    * Helper method to notify the renderers observing the TextureSet
130    * that the TextureSet has changed
131    */
132   void NotifyChangeToRenderers();
133
134 private:                               // Data
135   Vector<Render::Sampler*> mSamplers;  ///< List of samplers used by each texture. Not owned
136   Vector<Render::Texture*> mTextures;  ///< List of Textures. Not owned
137   Vector<Renderer*>        mRenderers; ///< List of renderers using the TextureSet
138   bool                     mHasAlpha;  ///< if any of the textures has an alpha channel
139 };
140
141 inline void SetTextureMessage(EventThreadServices& eventThreadServices, const TextureSet& textureSet, uint32_t index, Render::Texture* texture)
142 {
143   using LocalType = MessageValue2<TextureSet, uint32_t, Render::Texture*>;
144
145   // Reserve some memory inside the message queue
146   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
147
148   // Construct message in the message queue memory; note that delete should not be called on the return value
149   new(slot) LocalType(&textureSet, &TextureSet::SetTexture, index, texture);
150 }
151
152 inline void SetSamplerMessage(EventThreadServices& eventThreadServices, const TextureSet& textureSet, uint32_t index, Render::Sampler* sampler)
153 {
154   using LocalType = MessageValue2<TextureSet, uint32_t, Render::Sampler*>;
155
156   // Reserve some memory inside the message queue
157   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
158
159   // Construct message in the message queue memory; note that delete should not be called on the return value
160   new(slot) LocalType(&textureSet, &TextureSet::SetSampler, index, sampler);
161 }
162
163 } // namespace SceneGraph
164
165 } // namespace Internal
166
167 } // namespace Dali
168
169 #endif //  DALI_INTERNAL_SCENE_GRAPH_TEXTURE_SET_H