Added sampler properties, test cases.
[platform/core/uifw/dali-core.git] / dali / internal / update / effects / scene-graph-sampler.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_SAMPLER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_SAMPLER_H
3
4 /*
5  * Copyright (c) 2015 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 #include <dali/public-api/shader-effects/sampler.h>
21 #include <dali/integration-api/resource-declarations.h>
22 #include <dali/internal/event/common/event-thread-services.h>
23 #include <dali/internal/update/common/double-buffered.h>
24 #include <dali/internal/update/common/double-buffered-property.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/common/scene-graph-connection-observers.h>
27 #include <dali/internal/update/resources/bitmap-metadata.h>
28 #include <dali/internal/render/data-providers/sampler-data-provider.h>
29
30 #include <string>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace SceneGraph
37 {
38 class SceneController;
39
40 class Sampler : public PropertyOwner, public SamplerDataProvider
41 {
42 public:
43   typedef Dali::Sampler::FilterMode FilterMode;
44   typedef Dali::Sampler::WrapMode   WrapMode;
45
46   /**
47    * Constructor
48    */
49   Sampler( const std::string& samplerName );
50
51   /**
52    * Destructor
53    */
54   virtual ~Sampler();
55
56   /**
57    * Set the uniform name of this sampler. This allows the shader to find the
58    * GL index of this sampler.
59    */
60   void SetTextureUnitUniformName( const std::string& textureUnitUniformName );
61
62   /**
63    * Set the texture identity of this sampler (needs to double buffer this value because
64    * it can be read through the data provider interface in the render thread )
65    * @param[in] bufferIndex The buffer index to use
66    * @param[in] textureId The identity of the texture
67    */
68   void SetTexture( BufferIndex bufferIndex, Integration::ResourceId textureId );
69
70   /**
71    * Set the filter modes for minify and magnify filters
72    * @param[in] bufferIndex The buffer index to use
73    * @param[in] minFilter The minify filter
74    * @param[in] magFilter The magnify filter
75    */
76   void SetFilterMode( BufferIndex bufferIndex, FilterMode minFilter, FilterMode magFilter );
77
78   /**
79    * @param[in] bufferIndex The buffer index to use
80    */
81   void SetWrapMode( BufferIndex bufferIndex, WrapMode uWrap, WrapMode vWrap );
82
83   /**
84    * @param[in] bufferIndex The buffer index to use
85    * @return true if this sampler affects transparency of the material
86    * @note this should only be called from Update thread
87    */
88   bool AffectsTransparency( BufferIndex bufferIndex ) const;
89
90   /**
91    * Sets whether the associated texture is fully opaque or not.
92    * @param[in] fullyOpaque true if it's fully opaque
93    */
94   void SetFullyOpaque( bool fullyOpaque );
95
96   /**
97    * @param[in] bufferIndex The buffer index to use
98    * @return true if the texture is fully opaque
99    * @note this should only be called from Update thread
100    */
101   bool IsFullyOpaque( BufferIndex bufferIndex ) const;
102
103
104 public: // SamplerDataProvider interface - called from RenderThread
105   /**
106    * Get the texture unit uniform name
107    * @return the name of the texture unit uniform
108    */
109   virtual const std::string& GetTextureUnitUniformName() const;
110
111   /**
112    * Get the texture ID
113    * @param[in] bufferIndex The buffer index to use
114    * @return the identity of the associated texture
115    */
116   virtual Integration::ResourceId GetTextureId(BufferIndex buffer) const;
117
118   /**
119    * Get the filter mode
120    * @param[in] bufferIndex The buffer index to use
121    * @return The minify filter mode
122    */
123   virtual FilterMode GetMinifyFilterMode( BufferIndex bufferIndex ) const;
124
125   /**
126    * Get the filter mode
127    * @param[in] bufferIndex The buffer index to use
128    * @return The magnify filter mode
129    */
130   virtual FilterMode GetMagnifyFilterMode( BufferIndex bufferIndex ) const;
131
132   /**
133    * Get the horizontal wrap mode
134    * @param[in] bufferIndex The buffer index to use
135    * @return The horizontal wrap mode
136    */
137   virtual WrapMode GetUWrapMode( BufferIndex bufferIndex ) const;
138
139   /**
140    * Get the vertical wrap mode
141    * @param[in] bufferIndex The buffer index to use
142    * @return The vertical wrap mode
143    */
144   virtual WrapMode GetVWrapMode( BufferIndex bufferIndex ) const;
145
146   /**
147    * Connect the object to the scene graph
148    *
149    * @param[in] sceneController The scene controller - used for sending messages to render thread
150    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
151    */
152   void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
153
154   /**
155    * Disconnect the object from the scene graph
156    * @param[in] sceneController The scene controller - used for sending messages to render thread
157    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
158    */
159   void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
160   /**
161    * @copydoc ConnectionObservers::AddObserver
162    */
163   void AddConnectionObserver(ConnectionObservers::Observer& observer);
164
165   /**
166    * @copydoc ConnectionObservers::RemoveObserver
167    */
168   void RemoveConnectionObserver(ConnectionObservers::Observer& observer);
169
170 public: // PropertyOwner implementation
171   /**
172    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
173    */
174   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
175
176 public: // Properties
177   DoubleBufferedProperty<int>  mMinFilter;    ///< The minify filter
178   DoubleBufferedProperty<int>  mMagFilter;    ///< The magnify filter
179   DoubleBufferedProperty<int>  mUWrapMode;    ///< The horizontal wrap mode
180   DoubleBufferedProperty<int>  mVWrapMode;    ///< The vertical wrap mode
181   DoubleBufferedProperty<bool> mAffectsTransparency; ///< If this sampler affects renderer transparency
182
183 private:
184   std::string mTextureUnitUniformName; ///< The name of the uniform of the texture unit
185   DoubleBufferedProperty<unsigned int> mTextureId;
186   ConnectionObservers mConnectionObservers; ///< Connection observers that will be informed when textures change.
187   bool mFullyOpaque; // Update only flag - no need for double buffering
188 };
189
190 } // namespace SceneGraph
191
192 inline void SetTextureUnitUniformNameMessage( EventThreadServices& eventThreadServices, const SceneGraph::Sampler& sampler, const std::string& name )
193 {
194   typedef MessageValue1< SceneGraph::Sampler, std::string > LocalType;
195
196   // Reserve some memory inside the message queue
197   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
198
199   // Construct message in the message queue memory; note that delete should not be called on the return value
200   new (slot) LocalType( &sampler, &SceneGraph::Sampler::SetTextureUnitUniformName, name );
201 }
202
203
204 inline void SetTextureMessage( EventThreadServices& eventThreadServices, const SceneGraph::Sampler& sampler, unsigned int resourceId )
205 {
206   typedef MessageDoubleBuffered1< SceneGraph::Sampler, unsigned int > 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( &sampler, &SceneGraph::Sampler::SetTexture, resourceId );
213 }
214
215 // Declare enum as a message parameter type outside the SceneGraph namespace
216 template <> struct ParameterType< SceneGraph::Sampler::FilterMode > : public BasicType< SceneGraph::Sampler::FilterMode > {};
217
218
219 inline void SetFilterModeMessage( EventThreadServices& eventThreadServices, const SceneGraph::Sampler& sampler, SceneGraph::Sampler::FilterMode minFilter, SceneGraph::Sampler::FilterMode magFilter )
220 {
221   typedef MessageDoubleBuffered2< SceneGraph::Sampler, SceneGraph::Sampler::FilterMode, SceneGraph::Sampler::FilterMode > LocalType;
222
223   // Reserve some memory inside the message queue
224   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
225
226   // Construct message in the message queue memory; note that delete should not be called on the return value
227   new (slot) LocalType( &sampler, &SceneGraph::Sampler::SetFilterMode, minFilter, magFilter );
228 }
229
230 // Declare enum as a message parameter type
231 template <> struct ParameterType< SceneGraph::Sampler::WrapMode > : public BasicType< SceneGraph::Sampler::WrapMode > {};
232
233
234 inline void SetWrapModeMessage( EventThreadServices& eventThreadServices, const SceneGraph::Sampler& sampler, SceneGraph::Sampler::WrapMode horizontalWrap, SceneGraph::Sampler::WrapMode verticalWrap )
235 {
236   typedef MessageDoubleBuffered2< SceneGraph::Sampler, SceneGraph::Sampler::WrapMode, SceneGraph::Sampler::WrapMode > LocalType;
237
238   // Reserve some memory inside the message queue
239   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
240
241   // Construct message in the message queue memory; note that delete should not be called on the return value
242   new (slot) LocalType( &sampler, &SceneGraph::Sampler::SetWrapMode, horizontalWrap, verticalWrap );
243 }
244
245 } // namespace Internal
246 } // namespace Dali
247
248
249 #endif //  DALI_INTERNAL_SCENE_GRAPH_SAMPLER_H