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