Merge "Moved TextureCache dispatch methods to their own class" 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/devel-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 class Sampler;
36 }
37 namespace SceneGraph
38 {
39 class Renderer;
40 class Sampler;
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    * Return whether any texture in the texture set has an alpha channel
84    * @return true if at least one texture in the texture set has an alpha channel, false otherwise
85    */
86   bool HasAlpha() const;
87
88   /**
89    * Get the resource status
90    * Note, we need two values as it's possible that some resource failed to load
91    * in which case resourcesReady is false (the texture set is not good to be used for rendering)
92    * but finishedResourceAcquisition if true as there is no more loading going on
93    * @param[out] resourcesReady if the texture set is ready to be used for rendering
94    * @param[out] finishedResourceAcquisition if
95    */
96   void GetResourcesStatus( bool& resourcesReady, bool& finishedResourceAcquisition );
97
98   /**
99    * Adds a renderer to the Renderers list of the texture set.
100    * Renderers using the TextureSet get a notification when the texture set changes
101    *
102    * @param[in] renderer The renderer using the TextureSet
103    */
104   void AddObserver( Renderer* renderer );
105
106   /**
107    * Removes a renderer from the TextureSet renderers list
108    *
109    * @param[in] renderer The renderer no longer using the TextureSet
110    */
111   void RemoveObserver( Renderer* renderer );
112
113   /**
114    * Get the ResourceId of a texture in the TextureSet
115    * @param[in] index The index of the texture in the textures array
116    * @return the ResourceId
117    */
118   ResourceId GetTextureId( size_t index )
119   {
120     return mTextureId[index];
121   }
122
123   /**
124    * Get the sampler of a texture in the TextureSet
125    * @param[in] index The index of the texture in the textures array
126    * @return the sampler used by the texture
127    */
128   Render::Sampler* GetTextureSampler( size_t index )
129   {
130     return mSamplers[index];
131   }
132
133   /**
134    * Get the number of textures in the texture set
135    * @return The number of textures
136    */
137   size_t GetTextureCount()
138   {
139     return mTextureId.Size();
140   }
141
142
143 private:
144
145   /**
146    * Protected constructor; See also TextureSet::New()
147    */
148   TextureSet();
149
150   /**
151    * Helper method to notify the renderers observing the TextureSet
152    * that the TextureSet has changed
153    */
154   void NotifyChangeToRenderers();
155
156 private: // Data
157
158   Vector< Render::Sampler* >      mSamplers;                    ///< List of samplers used by each texture. Not owned
159   Vector< ResourceId >            mTextureId;                   ///< List of texture ids
160   Vector<Renderer*>               mRenderers;                   ///< List of renderers using the TextureSet
161   bool                            mResourcesReady;              ///< if the textures are ready to be used for rendering
162   bool                            mFinishedResourceAcquisition; ///< if resource loading is completed
163   bool                            mChanged;                     ///< if the texture set has changed since the last frame
164   bool                            mHasAlpha;                    ///< if any of the textures has an alpha channel
165 };
166
167 inline void SetImageMessage( EventThreadServices& eventThreadServices, const TextureSet& textureSet, size_t index, ResourceId resourceId )
168 {
169   typedef MessageValue2< TextureSet, size_t, ResourceId > LocalType;
170
171   // Reserve some memory inside the message queue
172   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
173
174   // Construct message in the message queue memory; note that delete should not be called on the return value
175   new (slot) LocalType( &textureSet, &TextureSet::SetImage, index, resourceId );
176 }
177
178 inline void SetSamplerMessage( EventThreadServices& eventThreadServices, const TextureSet& textureSet, size_t index, Render::Sampler* sampler )
179 {
180   typedef MessageValue2< TextureSet, size_t, Render::Sampler* > LocalType;
181
182   // Reserve some memory inside the message queue
183   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
184
185   // Construct message in the message queue memory; note that delete should not be called on the return value
186   new (slot) LocalType( &textureSet, &TextureSet::SetSampler, index, sampler );
187 }
188
189 } // namespace SceneGraph
190
191 } // namespace Internal
192
193 } // namespace Dali
194
195 #endif //  DALI_INTERNAL_SCENE_GRAPH_TEXTURE_SET_H