Merge "Added ability to build for coverage for target" into 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) 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/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 #include <dali/internal/update/resources/resource-manager-declarations.h>
26
27 namespace Dali
28 {
29 namespace Internal
30 {
31 class ResourceManager;
32
33 namespace Render
34 {
35 struct Sampler;
36 class NewTexture;
37 }
38 namespace SceneGraph
39 {
40 class Renderer;
41
42 class TextureSet
43 {
44 public:
45
46   /**
47    * Construct a new TextureSet.
48    */
49   static TextureSet* New();
50
51   /**
52    * Destructor
53    */
54   virtual ~TextureSet();
55
56   /**
57    * Overriden delete operator
58    * Deletes the texture set from its global memory pool
59    */
60   void operator delete( void* ptr );
61
62   /**
63    * Prepare the texture set, check texture loading status, opacity etc
64    * @param resourceManager for checking texture details and loading status
65    */
66   void Prepare( const ResourceManager& resourceManager );
67
68   /**
69    * Set the resource id for the texture at position "index"
70    * @param[in] index The index of the texture
71    * @param[in] imageId the resource id of the imag
72    */
73   void SetImage( size_t index,  ResourceId imageId );
74
75   /**
76    * Set the sampler to be used by the texture at position "index"
77    * @param[in] index The index of the texture
78    * @param[in] sampler The sampler to be used by the texture
79    */
80   void SetSampler( size_t index, Render::Sampler* sampler );
81
82   /**
83    * Set the texture at position "index"
84    * @param[in] index The index of the texture
85    * @param[in] texture The texture
86    */
87   void SetTexture( size_t index, Render::NewTexture* texture );
88
89   /**
90    * Return whether any texture in the texture set has an alpha channel
91    * @return true if at least one texture in the texture set has an alpha channel, false otherwise
92    */
93   bool HasAlpha() const;
94
95   /**
96    * Get the resource status
97    * Note, we need two values as it's possible that some resource failed to load
98    * in which case resourcesReady is false (the texture set is not good to be used for rendering)
99    * but finishedResourceAcquisition if true as there is no more loading going on
100    * @param[out] resourcesReady if the texture set is ready to be used for rendering
101    * @param[out] finishedResourceAcquisition if
102    */
103   void GetResourcesStatus( bool& resourcesReady, bool& finishedResourceAcquisition );
104
105   /**
106    * Adds a renderer to the Renderers list of the texture set.
107    * Renderers using the TextureSet get a notification when the texture set changes
108    *
109    * @param[in] renderer The renderer using the TextureSet
110    */
111   void AddObserver( Renderer* renderer );
112
113   /**
114    * Removes a renderer from the TextureSet renderers list
115    *
116    * @param[in] renderer The renderer no longer using the TextureSet
117    */
118   void RemoveObserver( Renderer* renderer );
119
120   /**
121    * Get the ResourceId of a texture in the TextureSet
122    * @param[in] index The index of the texture in the textures array
123    * @return the ResourceId
124    */
125   ResourceId GetTextureId( size_t index )
126   {
127     return mTextureId[index];
128   }
129
130   /**
131    * Get the sampler of a texture in the TextureSet
132    * @param[in] index The index of the texture in the textures array
133    * @return the sampler used by the texture
134    */
135   Render::Sampler* GetTextureSampler( size_t index )
136   {
137     return mSamplers[index];
138   }
139
140   /**
141    * Get the number of textures in the texture set
142    * @return The number of textures
143    */
144   size_t GetTextureCount()
145   {
146     return mTextureId.Size();
147   }
148
149   /**
150    * Get the number of NewTextures in the texture set
151    * @return The number of NewTextures
152    */
153   size_t GetNewTextureCount()
154   {
155     return mTextures.Size();
156   }
157
158   /**
159    * Get the pointer to  a NewTexture in the TextureSet
160    * @param[in] index The index of the texture in the textures array
161    * @return the pointer to the NewTexture in that position
162    */
163   Render::NewTexture* GetNewTexture( size_t index )
164   {
165     return mTextures[index];
166   }
167
168 private:
169
170   /**
171    * Protected constructor; See also TextureSet::New()
172    */
173   TextureSet();
174
175   /**
176    * Helper method to notify the renderers observing the TextureSet
177    * that the TextureSet has changed
178    */
179   void NotifyChangeToRenderers();
180
181 private: // Data
182
183   Vector< Render::Sampler* >      mSamplers;                    ///< List of samplers used by each texture. Not owned
184   Vector< Render::NewTexture* >   mTextures;                    ///< List of NewTextures. Not owned
185   Vector< ResourceId >            mTextureId;                   ///< List of texture ids
186   Vector<Renderer*>               mRenderers;                   ///< List of renderers using the TextureSet
187   bool                            mResourcesReady;              ///< if the textures are ready to be used for rendering
188   bool                            mFinishedResourceAcquisition; ///< if resource loading is completed
189   bool                            mChanged;                     ///< if the texture set has changed since the last frame
190   bool                            mHasAlpha;                    ///< if any of the textures has an alpha channel
191 };
192
193 inline void SetImageMessage( EventThreadServices& eventThreadServices, const TextureSet& textureSet, size_t index, ResourceId resourceId )
194 {
195   typedef MessageValue2< TextureSet, size_t, ResourceId > LocalType;
196
197   // Reserve some memory inside the message queue
198   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
199
200   // Construct message in the message queue memory; note that delete should not be called on the return value
201   new (slot) LocalType( &textureSet, &TextureSet::SetImage, index, resourceId );
202 }
203
204 inline void SetTextureMessage( EventThreadServices& eventThreadServices, const TextureSet& textureSet, size_t index, Render::NewTexture* texture )
205 {
206   typedef MessageValue2< TextureSet, size_t, Render::NewTexture* > LocalType;
207
208   // Reserve some memory inside the message queue
209   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
210
211   // Construct message in the message queue memory; note that delete should not be called on the return value
212   new (slot) LocalType( &textureSet, &TextureSet::SetTexture, index, texture );
213 }
214
215 inline void SetSamplerMessage( EventThreadServices& eventThreadServices, const TextureSet& textureSet, size_t index, Render::Sampler* sampler )
216 {
217   typedef MessageValue2< TextureSet, size_t, Render::Sampler* > LocalType;
218
219   // Reserve some memory inside the message queue
220   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
221
222   // Construct message in the message queue memory; note that delete should not be called on the return value
223   new (slot) LocalType( &textureSet, &TextureSet::SetSampler, index, sampler );
224 }
225
226 } // namespace SceneGraph
227
228 } // namespace Internal
229
230 } // namespace Dali
231
232 #endif //  DALI_INTERNAL_SCENE_GRAPH_TEXTURE_SET_H