Rendering API clean-up
[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) 2016 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/devel-api/rendering/texture-set.h>
22 #include <dali/internal/common/buffer-index.h>
23 #include <dali/internal/event/common/event-thread-services.h>
24 #include <dali/internal/update/common/animatable-property.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/common/scene-graph-connection-change-propagator.h>
27 #include <dali/internal/update/common/uniform-map.h>
28 #include <dali/internal/update/resources/resource-manager-declarations.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 class ResourceManager;
35
36 namespace Render
37 {
38 class Sampler;
39 }
40 namespace SceneGraph
41 {
42 class Sampler;
43 class Shader;
44 class ConnectionObserver;
45 class SceneController;
46
47 class TextureSet : public PropertyOwner, public UniformMap::Observer
48 {
49 public:
50
51   /**
52    * Construct a new TextureSet.
53    */
54   static TextureSet* New();
55
56   /**
57    * Destructor
58    */
59   virtual ~TextureSet();
60
61   /**
62    * Overriden delete operator
63    * Deletes the texture set from its global memory pool
64    */
65   void operator delete( void* ptr );
66
67   /**
68    * Prepare the texture set, check texture loading status, opacity etc
69    * @param resourceManager for checking texture details and loading status
70    */
71   void Prepare( const ResourceManager& resourceManager );
72
73   /**
74    * Set the resource id for the texture at position "index"
75    * @param[in] index The index of the texture
76    * @param[in] imageId the resource id of the imag
77    */
78   void SetImage( size_t index,  ResourceId imageId );
79
80   /**
81    * Set the sampler to be used by the texture at position "index"
82    * @param[in] index The index of the texture
83    * @param[in] sampler The sampler to be used by the texture
84    */
85   void SetSampler( size_t index, Render::Sampler* sampler );
86
87   /**
88    * Return whether any texture in the texture set has an alpha channel
89    * @return true if at least one texture in the texture set has an alpha channel, false otherwise
90    */
91   bool HasAlpha() const;
92
93   /**
94    * Get the resource status
95    * Note, we need two values as it's possible that some resource failed to load
96    * in which case resourcesReady is false (the texture set is not good to be used for rendering)
97    * but finishedResourceAcquisition if true as there is no more loading going on
98    * @param[out] resourcesReady if the texture set is ready to be used for rendering
99    * @param[out] finishedResourceAcquisition if
100    */
101   void GetResourcesStatus( bool& resourcesReady, bool& finishedResourceAcquisition );
102
103
104 public: // Implementation of ObjectOwnerContainer template methods
105
106   /**
107    * Connect the object to the scene graph
108    *
109    * @param[in] sceneController The scene controller - used for sending messages to render thread
110    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
111    */
112   void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
113
114   /**
115    * Disconnect the object from the scene graph
116    * @param[in] sceneController The scene controller - used for sending messages to render thread
117    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
118    */
119   void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
120
121 public: // Implementation of ConnectionChangePropagator
122
123   /**
124    * @copydoc ConnectionChangePropagator::AddObserver
125    */
126   void AddConnectionObserver(ConnectionChangePropagator::Observer& observer);
127
128   /**
129    * @copydoc ConnectionChangePropagator::RemoveObserver
130    */
131   void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer);
132
133 public:
134
135   /**
136    * Get the ResourceId of a texture in the TextureSet
137    * @param[in] index The index of the texture in the textures array
138    * @return the ResourceId
139    */
140   ResourceId GetTextureId( size_t index )
141   {
142     return mTextureId[index];
143   }
144
145   /**
146    * Get the sampler of a texture in the TextureSet
147    * @param[in] index The index of the texture in the textures array
148    * @return the sampler used by the texture
149    */
150   Render::Sampler* GetTextureSampler( size_t index )
151   {
152     return mSamplers[index];
153   }
154
155   /**
156    * Get the number of textures in the texture set
157    * @return The number of textures
158    */
159   size_t GetTextureCount()
160   {
161     return mTextureId.Size();
162   }
163
164 public: // UniformMap::Observer
165   /**
166    * @copydoc UniformMap::Observer::UniformMappingsChanged
167    */
168   virtual void UniformMappingsChanged( const UniformMap& mappings );
169
170 private:
171
172   /**
173    * Protected constructor; See also TextureSet::New()
174    */
175   TextureSet();
176
177 private: // Data
178
179   Vector< Render::Sampler* >      mSamplers; // Not owned
180   Vector< ResourceId >            mTextureId;
181   ConnectionChangePropagator      mConnectionObservers;
182   bool                            mResourcesReady; ///< if the textures are ready to be used for rendering
183   bool                            mFinishedResourceAcquisition; ///< if resource loading is completed
184   bool                            mChanged; ///< if the texture set has changed since the last frame
185   bool                            mHasAlpha; ///< if any of the textures has an alpha channel
186 };
187
188 inline void SetImageMessage( EventThreadServices& eventThreadServices, const TextureSet& textureSet, size_t index, ResourceId resourceId )
189 {
190   typedef MessageValue2< TextureSet, size_t, ResourceId > LocalType;
191
192   // Reserve some memory inside the message queue
193   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
194
195   // Construct message in the message queue memory; note that delete should not be called on the return value
196   new (slot) LocalType( &textureSet, &TextureSet::SetImage, index, resourceId );
197 }
198
199 inline void SetSamplerMessage( EventThreadServices& eventThreadServices, const TextureSet& textureSet, size_t index, Render::Sampler* sampler )
200 {
201   typedef MessageValue2< TextureSet, size_t, Render::Sampler* > LocalType;
202
203   // Reserve some memory inside the message queue
204   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
205
206   // Construct message in the message queue memory; note that delete should not be called on the return value
207   new (slot) LocalType( &textureSet, &TextureSet::SetSampler, index, sampler );
208 }
209
210 } // namespace SceneGraph
211
212 } // namespace Internal
213
214 } // namespace Dali
215
216 #endif //  DALI_INTERNAL_SCENE_GRAPH_TEXTURE_SET_H